猿问

如何使用谷歌图表在条形图顶部插入点?

给定插图

我能够使用以下代码创建图表(没有点):


<html>

  <head>

      <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

    <script type="text/javascript">

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

      google.charts.setOnLoadCallback(drawStuff);


      var chart;


      function drawStuff() {

        var data = new google.visualization.arrayToDataTable([

          ['Y', 'Series 1','Series 2'],

          ["Element A", 44,11],

          ["Element B", 31,11],

          ["Element C", 12,11],

        ]);


        var options = {

          bars: 'horizontal', // Required for Material Bar Charts.

          axes: {

            x: {

              0: { side: 'bottom', label: 'X'} // Top x-axis.

            }

          }

        };


        var div = document.getElementById('top_x_div');

        var link = document.getElementById('url');

        chart = new google.charts.Bar(div);

        chart.draw(data, options);

        //console.log(chart.getImageURI());

      };

    </script>

  </head>

  <body>

    <div id="top_x_div" style="width: 900px; height: 500px;"></div>

  </body>

</html>

如何使用谷歌图表在条形图顶部插入点?


天涯尽头无女友
浏览 144回答 1
1回答

蝴蝶不菲

我想到了几种方法,来完成...使用组合图在图表的事件上手动添加点'ready'但是,使用材料图表无法实现这两个选项。google.charts.Bar首先,材料图表不支持组合图表。并且,材料图表没有手动添加点所需的方法。更不用说,还有几种选项是材料图表不支持的。请参见 -->&nbsp;材料图表特征奇偶校验的跟踪问题 #2143所以我们必须使用经典的图表...google.visualization.BarChart我们可以使用一个选项来使经典图表具有与材料图表相似的外观和感觉......theme:&nbsp;'material'现在的解决方案...尝试使用组合图时,这些点与两条形之间的 y 轴对齐。没有一个选项可以允许点在条形上对齐。因此,我们必须在图表的事件上手动添加点...'ready'我们需要的图表方法很少...getChartLayoutInterface()- 返回一个对象,其中包含有关图表及其元素在屏幕上的位置的信息。getBoundingBox(id)- 返回包含图表元素 id 的左、顶、宽和高的对象。getXLocation(position, optional_axis_index)- 返回相对于图表容器的位置的屏幕 x 坐标。一旦图表的事件触发,我们可以使用 -->'ready'getChartLayoutInterface()上面提到的另外两种方法是布局界面的方法。我们用来获取柱的坐标,该点将被放置在该柱上。这将给我们Y坐标和点的高度。条形图的 id 由数据行和序列列确定。getBoundingBox(id)bar#C#R&nbsp;&nbsp;//&nbsp;where&nbsp;C&nbsp;is&nbsp;the&nbsp;series&nbsp;column&nbsp;index,&nbsp;and&nbsp;R&nbsp;is&nbsp;the&nbsp;row&nbsp;index我们提供X坐标,将提供图表上的位置,需要将点放在x轴上,在我们预定的位置。getXLocation// x coordinates of points to addvar points = [&nbsp; [36, 8],&nbsp; [28, 8],&nbsp; [10, 8],];// loop data rows and columnsfor (var r = 0; r < data.getNumberOfRows(); r++) {&nbsp; for (var c = 1; c < data.getNumberOfColumns(); c++) {&nbsp; &nbsp; // calculate position of the point&nbsp; &nbsp; var barBounds = chartLayout.getBoundingBox('bar#' + (c - 1) + '#' + r);&nbsp; &nbsp; var xCoord = chartLayout.getXLocation(points[r][c - 1]);&nbsp; &nbsp; var height = barBounds.height / 4;&nbsp; &nbsp; var top = barBounds.top + (height * 2);&nbsp; &nbsp; // create and add the point to the chart&nbsp; &nbsp; var point = document.createElementNS(svgNS, 'circle');&nbsp; &nbsp; point.setAttribute('cx', xCoord);&nbsp; &nbsp; point.setAttribute('cy', top);&nbsp; &nbsp; point.setAttribute('r', height);&nbsp; &nbsp; point.setAttribute('stroke', '#ffffff');&nbsp; &nbsp; point.setAttribute('stroke-width', height);&nbsp; &nbsp; point.setAttribute('fill', 'transparent');&nbsp; &nbsp; svg.appendChild(point);&nbsp; }}请参阅以下工作片段...var chart;google.charts.load('current', {&nbsp; packages: ['corechart']}).then(function drawStuff() {&nbsp; var data = new google.visualization.arrayToDataTable([&nbsp; &nbsp; ['Y', 'Series 1', 'Series 2'],&nbsp; &nbsp; ['Element A', 44, 11],&nbsp; &nbsp; ['Element B', 31, 11],&nbsp; &nbsp; ['Element C', 12, 11],&nbsp; ]);&nbsp; // x coordinates of points to add&nbsp; var points = [&nbsp; &nbsp; [36, 8],&nbsp; &nbsp; [28, 8],&nbsp; &nbsp; [10, 8],&nbsp; ];&nbsp; var options = {&nbsp; &nbsp; chartArea: {&nbsp; &nbsp; &nbsp; left: 128,&nbsp; &nbsp; &nbsp; top: 24,&nbsp; &nbsp; &nbsp; right: 128,&nbsp; &nbsp; &nbsp; bottom: 72,&nbsp; &nbsp; &nbsp; height: '100%',&nbsp; &nbsp; &nbsp; width: '100%'&nbsp; &nbsp; },&nbsp; &nbsp; hAxis: {&nbsp; &nbsp; &nbsp; title: 'X'&nbsp; &nbsp; },&nbsp; &nbsp; height: '100%',&nbsp; &nbsp; theme: 'material',&nbsp; &nbsp; vAxis: {&nbsp; &nbsp; &nbsp; title: data.getColumnLabel(0)&nbsp; &nbsp; },&nbsp; &nbsp; width: '100%'&nbsp; };&nbsp; var div = document.getElementById('top_x_div');&nbsp; //var link = document.getElementById('url');&nbsp; chart = new google.visualization.BarChart(div);&nbsp; google.visualization.events.addListener(chart, 'ready', function () {&nbsp; &nbsp; // get chart layour interface and svg&nbsp; &nbsp; var chartLayout = chart.getChartLayoutInterface();&nbsp; &nbsp; var svg = div.getElementsByTagName('svg')[0];&nbsp; &nbsp; var svgNS = svg.namespaceURI;&nbsp; &nbsp; // loop data rows and columns&nbsp; &nbsp; for (var r = 0; r < data.getNumberOfRows(); r++) {&nbsp; &nbsp; &nbsp; for (var c = 1; c < data.getNumberOfColumns(); c++) {&nbsp; &nbsp; &nbsp; &nbsp; // calculate position of the point&nbsp; &nbsp; &nbsp; &nbsp; var barBounds = chartLayout.getBoundingBox('bar#' + (c - 1) + '#' + r);&nbsp; &nbsp; &nbsp; &nbsp; var xCoord = chartLayout.getXLocation(points[r][c - 1]);&nbsp; &nbsp; &nbsp; &nbsp; var height = barBounds.height / 4;&nbsp; &nbsp; &nbsp; &nbsp; var top = barBounds.top + (height * 2);&nbsp; &nbsp; &nbsp; &nbsp; // create and add the point to the chart&nbsp; &nbsp; &nbsp; &nbsp; var point = document.createElementNS(svgNS, 'circle');&nbsp; &nbsp; &nbsp; &nbsp; point.setAttribute('cx', xCoord);&nbsp; &nbsp; &nbsp; &nbsp; point.setAttribute('cy', top);&nbsp; &nbsp; &nbsp; &nbsp; point.setAttribute('r', height);&nbsp; &nbsp; &nbsp; &nbsp; point.setAttribute('stroke', '#ffffff');&nbsp; &nbsp; &nbsp; &nbsp; point.setAttribute('stroke-width', height);&nbsp; &nbsp; &nbsp; &nbsp; point.setAttribute('fill', 'transparent');&nbsp; &nbsp; &nbsp; &nbsp; svg.appendChild(point);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; });&nbsp; chart.draw(data, options);&nbsp; window.addEventListener('resize', function () {&nbsp; &nbsp; chart.draw(data, options);&nbsp; });});#top_x_div {&nbsp; height: 400px;}<script src="https://www.gstatic.com/charts/loader.js"></script><div id="top_x_div"></div>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答