简体   繁体   English

如何使用Google Charts API实现水平烛台?

[英]How do you implement horizontal candlesticks using the Google Charts API?

I'd like to use the Google Charts API, but they don't seem to support this (at least publicly). 我想使用Google Charts API,但他们似乎(至少在公开场合)不支持此功能。

This is what I'd like to replicate (for an internal report): screenshot 这就是我要复制的内容(用于内部报告): 屏幕截图

Edit: This is the closest thing that I've found: https://developers.google.com/chart/interactive/docs/reference#barformatter 编辑:这是我发现的最接近的东西: https : //developers.google.com/chart/interactive/docs/reference#barformatter

There is no easy way to do exactly the same thing, but you can cheat a bit and get something like this (you can customize as needed for further fine tuning): 要实现完全相同的操作,没有简单的方法,但是您可以作弊并获得类似的信息(可以根据需要进行自定义以进行进一步的微调):

样品

Basically, I make a stacked bar chart with 6 different series. 基本上,我制作了6种不同系列的堆积条形图。

  1. Dummy (transparent) series 虚拟(透明)系列
  2. Black Lines (so the error bars appear properly) 黑线(因此错误栏会正确显示)
  3. (and 4, 5, 6) Grey, Red, Yellow, Green for the ranges (和4,5,6)灰色,红色,黄色,绿色作为范围

The Dummy series is transparent, and sets how far up the chart the first bar appears. “虚拟”系列是透明的,并设置第一个条形图出现在图表的上方。 The black lines series is always 0, and has error bars on it that go from 0 to 100 (the interval columns below). 黑线系列始终为0,并带有从0到100的误差线(下面的间隔列)。 The others represent the data. 其他代表数据。

This is a bit complex to do by hand, but you can create a javascript function to appropriately distribute the numbers to the various series given a single value. 手工完成起来有点复杂,但是您可以创建一个javascript函数,以在给定单个值的情况下将数字适当地分配到各个系列中。 I just put in arbitrary values. 我只是输入任意值。

Colors and transparency can be optimized, as can the 'height' of each bar to make it look similar. 颜色和透明度可以优化,每个条的“高度”也可以使其相似。 The axes base lines, and the grid lines can be hidden to make it look better. 轴的基线和网格线可以隐藏以使其看起来更好。 The legend can also be hidden. 图例也可以隐藏。 I didn't do those figuring you'll fiddle with the code anyhow. 我没有做那些让您弄乱代码的事情。

Here is the code: 这是代码:

function drawVisualization() {
  // Create and populate the data table.
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Label');
  data.addColumn('number', 'Dummy');
  data.addColumn('number', 'Black for Lines');
  data.addColumn({type: 'number', role: 'interval'});
  data.addColumn({type: 'number', role: 'interval'});
  data.addColumn('number', 'Grey');
  data.addColumn('number', 'Red');
  data.addColumn('number', 'Yellow');
  data.addColumn('number', 'Green');
  data.addRows([
    ['Target', 65, 0, 0, 100, 15, 00, 00, 00],
    ['Test A', 85, 0, 0, 100, 00, 00, 00, 15],
    ['Test B', 70, 0, 0, 100, 10, 00, 05, 00],
    ['Test C', 10, 0, 0, 100, 00, 15, 00, 00],
  ]);

  // Create and draw the visualization.
  new google.visualization.BarChart(document.getElementById('visualization')).
      draw(data,
           {isStacked: true,           
            width:600, height:400,
            series: [{color: 'transparent'}, {color: 'black'}, {color: 'grey'}, {color: 'red'}, {color: 'yellow'}, {color: 'green'}]
           }
      );
}

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

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