javascript - 注释 Google 图表 API

标签 javascript charts google-visualization candlestick-chart waterfall

我正在尝试使用 Google Chart API 来构建瀑布图。我注意到烛台/瀑布图不支持注释。

See this jsfiddle sample

google.charts.load('current', {'packages':['corechart']});
  google.charts.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = new google.visualization.DataTable();
            data.addColumn('string', 'Category');
    data.addColumn('number', 'MinimumLevel');
    data.addColumn('number', 'MinimumLevel1');
    data.addColumn('number', 'MaximumLevel');
    data.addColumn('number', 'MaximumLevel1');
    data.addColumn({type: 'number', role: 'tooltip'});
    data.addColumn({type: 'string', role: 'style'});
    data.addColumn({type: 'number', role: 'annotation'});

    data.addRow(['Category 1', 0 , 0, 5, 5, 5,'gray',5]);
    data.addRow(['Category 2', 5 , 5, 10, 10, 10,'red',10]);
    data.addRow(['Category 3', 10 , 10, 15, 15, 15,'blue',15]);
    data.addRow(['Category 4', 15 , 15, 10, 10, 10,'yellow',10]);
    data.addRow(['Category 5', 10 , 10, 5, 5, 5,'gray',5]);

    var options = {
      legend: 'none',
      bar: { groupWidth: '60%' } // Remove space between bars.
    };

    var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }

我想将第五列的值放在每个烛台的顶部。 它应该看起来像这样: enter image description here

有办法做到这一点吗?

谢谢

最佳答案

我通过向隐藏散点图添加注释来向烛台图添加注释。您可以通过更改绘图来准确设置注释所在的位置。

    google.charts.load('current', { 'packages': ['corechart'] });
    google.charts.setOnLoadCallback(drawChart);

    function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('date', 'Date');
        data.addColumn('number', 'Low');
        data.addColumn('number', 'Open');
        data.addColumn('number', 'Close');
        data.addColumn('number', 'High');
        data.addColumn('number'); //scatter plot for annotations
        data.addColumn({ type: 'string', role: 'annotation' }); // annotation role col.
        data.addColumn({ type: 'string', role: 'annotationText' }); // annotationText col.
        var high, low, open, close = 160;
        for (var i = 0; i < 10; i++) {
            open = close;
            close += ~~(Math.random() * 10) * Math.pow(-1, ~~(Math.random() * 2));
            high = Math.max(open, close) + ~~(Math.random() * 10);
            low = Math.min(open, close) - ~~(Math.random() * 10);
            annotation = '$' + close;
            annotation_text = 'Close price: $' + close;
            data.addRow([new Date(2014, 0, i + 1), low, open, close, high, high, annotation, annotation_text]);
        }

        var view = new google.visualization.DataView(data);
        var chart = new google.visualization.ComboChart(document.querySelector('#chart_div'));

        chart.draw(view, {
            height: 400,
            width: 600,
            explorer: {},
            chartArea: {
                left: '7%',
                width: '70%'
            },
            series: {
                0: {
                    color: 'black',
                    type: 'candlesticks',
                },
                1: {
                    type: 'scatter',
                    pointSize: 0,
                    targetAxisIndex: 0,
                },
            },
            candlestick: {
                color: '#a52714',
                fallingColor: { strokeWidth: 0, fill: '#a52714' }, // red
                risingColor: { strokeWidth: 0, fill: '#0f9d58' } // green
            },
        });

    }
<script type="text/javascript"src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div" style="width: 900px; height: 500px;"></div>

关于javascript - 注释 Google 图表 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41855148/

相关文章:

javascript - 如何为三组数据创建堆积条形图?

javascript - 使用谷歌应用程序脚本将服务器端数据表返回给客户端

javascript - 使用 Vue.js 和express.js 交付 Html 文件

需要 Javascript 帮助 - 哪个变量返回空?

javascript - Angular 图表无法让标签按我需要的方式工作

Objective-C 图表框架

charts - 如何将 Flot 与 AngularJS 集成?

javascript - 带切片器的 Google 图表返回 : One or more participants failed to draw()×

google-maps - 在谷歌地图/可视化上可视化大量数据

javascript - 如何从同一个类中的另一个函数调用函数