google charts graphs set legend value and same color columns

你离开我真会死。 提交于 2019-12-13 18:13:02

问题


data = [["product1",3.6126719999999977],["product2",6.827597999999999],["product2",1008.0]] when i use array data and implement in columns or bar charts that time is displayed legend as value only not 'diffrent product name' and all bar charts look same color columns. i want to add legend name as product name and diffrent color to all columns

    google.charts.load('current', {
      packages: ['corechart']
    }).then(function () {
     

      
     $.ajax({
        url: 'path to data',
        dataType: 'json'
      }).done(function (jsonData) {
        loadData(jsonData);
      }).fail(function (jqXHR, textStatus, errorThrown) {
        var jsonData = [["product1",450],["product2",23],["product3",1008.0]];
        loadData(jsonData, '1', 'Column');
      });

      // load json data
      function loadData(jsonData, id, chartType) {
        // create data table
        var dataTable = new google.visualization.DataTable();

        switch (chartType) {
          case 'Column':
            // add date column
            dataTable.addColumn('string', 'Product');
            dataTable.addColumn('number', 'Value');

            $.each(jsonData, function(productIndex, product) {
              // add product data
              dataTable.addRow([
                product[0],
                product[1],
              ]);
            });
            break;

         
        }

        // draw chart
        $(window).resize(function () {
          drawChart(id, chartType, dataTable);
        });
        drawChart(id, chartType, dataTable);
      }

      // save charts for redraw
      var charts = {};
      var options = {
        Column: {
          chartArea: {
            height: '100%',
            width: '100%',
            top: 64,
            left: 64,
            right: 32,
            bottom: 48
          },
          height: '100%',
          legend: {
            position: 'top'
          },
          pointSize: 4,
          width: '100%'
        },
        Pie: {
          height: '100%',
          width: '100%'
        },
        Line: {
          height: '100%',
          width: '100%',
          legend: {
            position: 'bottom'
          },
          pointSize: 4,
          width: '100%'
        }

      };

      // draw chart
       function drawChart(id, chartType, dataTable) {
        if (!charts.hasOwnProperty(id)) {
          charts[id] = new google.visualization.ChartWrapper({
            chartType: chartType + 'Chart',
            containerId: 'chart-' + id,
            options: options[chartType]
          });
        }
        charts[id].setDataTable(dataTable);
        charts[id].draw();
      }
    });
    html, body {
      height: 100%;
      margin: 0px 0px 0px 0px;
      overflow: hidden;
      padding: 0px 0px 0px 0px;
    }

    .chart {
      display: inline-block;
      height: 100%;
      width: 32%;
    }
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div class="chart" id="chart-0"></div>
    <div class="chart" id="chart-1"></div>
    <div class="chart" id="chart-2"></div>

回答1:


in this case, you want to create columns for each product,
instead of rows for each product...

see following working snippet...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  // save charts for redraw
  var charts = {};
  var options = {
    Column: {
      chartArea: {
        height: '100%',
        width: '100%',
        top: 64,
        left: 64,
        right: 32,
        bottom: 48
      },
      height: '100%',
      legend: {
        position: 'top'
      },
      pointSize: 4,
      width: '100%'
    },
    Pie: {
      height: '100%',
      width: '100%'
    },
    Line: {
      height: '100%',
      width: '100%',
      legend: {
        position: 'bottom'
      },
      pointSize: 4,
      width: '100%'
    }
  };

  var jsonData = [["product1",450],["product2",23],["product3",1008.0]];
  loadData(jsonData, '1', 'Column');

  // load json data
  function loadData(jsonData, id, chartType) {
    // create data table
    var dataTable = new google.visualization.DataTable();

    switch (chartType) {
      case 'Column':
        // add date column
        dataTable.addColumn('string', 'Category');
        var rowIndex = dataTable.addRow();
        dataTable.setValue(rowIndex, 0, dataTable.getColumnLabel(0));

        $.each(jsonData, function(productIndex, product) {
          var colIndex = dataTable.addColumn('number', product[0]);
          // add product data
          dataTable.setValue(rowIndex, colIndex, product[1]);
        });
        break;
    }

    // draw chart
    $(window).resize(function () {
      drawChart(id, chartType, dataTable);
    });
    drawChart(id, chartType, dataTable);
  }

  // draw chart
  function drawChart(id, chartType, dataTable) {
    if (!charts.hasOwnProperty(id)) {
      charts[id] = new google.visualization.ChartWrapper({
        chartType: chartType + 'Chart',
        containerId: 'chart-' + id,
        options: options[chartType]
      });
    }
    charts[id].setDataTable(dataTable);
    charts[id].draw();
  }
});
html, body {
  height: 100%;
  margin: 0px 0px 0px 0px;
  overflow: hidden;
  padding: 0px 0px 0px 0px;
}

.chart {
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://www.gstatic.com/charts/loader.js"></script>
<div class="chart" id="chart-1"></div>


来源:https://stackoverflow.com/questions/49945386/google-charts-graphs-set-legend-value-and-same-color-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!