简体   繁体   English

如何在Google图表中更改条形图的方向

[英]How to change the bar chart orientation in Google Charts

I need to draw two bar charts facing each other in one page. 我需要在一页中绘制两个彼此相对的条形图。 (The chart on the left faces right, and the one on the right faces left). (左图为右图,右图为左图)。

But with Google Charts, I only managed to make both charts face right. 但是,使用Google图表,我只能使两个图表正确显示。

Is it possible to implement? 有可能实现吗? What should I do? 我该怎么办?

using a 100% stacked chart, you can get the bars to align to the right 使用100%堆叠的图表,您可以使条形图向右对齐

then color the first series transparent, 然后为第一个系列上色透明,
and manipulate the data to reveal the proper length 并处理数据以显示适当的长度

then when the chart's 'ready' event fires, 然后,当图表的'ready'事件触发时,
you can move around the chart elements, 您可以在图表元素周围移动,
such as the y-axis, and the order of the x-axis labels 例如y轴和x轴标签的顺序

first, you need to allow enough room on the left, 首先,您需要在左侧留出足够的空间,
for the original y-axis labels to print 用于打印原始y轴标签
otherwise they will be cutoff, ie 否则它们将被切断,即

Canis Major Dwarf vs. Canis Maj... Canis Major Dwarf对阵Canis Maj...

then need to leave enough room on the right, 然后需要在右侧留出足够的空间,
by limiting the chartArea , otherwise cutoff and simply not visible, ie 通过限制chartArea ,否则将截止并且根本不可见,即

Canis Major Dwarf vs. Canis M Canis Major Dwarf vs. Canis M

might be easier, providing your own labels 提供自己的标签可能会更容易

this should give you something to tweak on... 这应该给你一些调整...

 google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawStuff); function drawStuff() { var data = new google.visualization.arrayToDataTable([ ['Galaxy', 'Distance', 'Brightness'], ['Canis Major Dwarf', 10, 20], ['Sagittarius Dwarf', 20, 40], ['Ursa Major II Dwarf', 40, 50], ['Lg. Magellanic Cloud', 60, 80], ['Bootes I', 80, 120] ]); var options = { isStacked: 'percent', colors: ['transparent', 'magenta'], legend: { position: 'bottom' }, chartArea: { left: 200, width: 400 }, width: 800 }; var container = document.getElementById('dual_x_div'); var chart = new google.visualization.BarChart(container); google.visualization.events.addListener(chart, 'ready', function () { var labels = container.getElementsByTagName('text'); var hAxisLabels = []; Array.prototype.forEach.call(labels, function (text, index) { switch (text.getAttribute('text-anchor')) { // move y axis labels case 'end': text.setAttribute('x', parseFloat(text.getAttribute('x')) + 540); break; // save x axis labels case 'middle': // save x position here // otherwise, x position will change // before you know where the next should have been hAxisLabels.push({ text: text, x: parseFloat(text.getAttribute('x')) }); break; } }); // swap label positions hAxisLabels.forEach(function (label, index) { label.text.setAttribute('x', hAxisLabels[hAxisLabels.length - index - 1].x); }); }); chart.draw(data, options); }; 
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="dual_x_div"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM