如何从一个js函数创建多个折线图

它在

我正在尝试将Google折线图用作我的flask项目的一部分。但是我不只是想要一个图,而是有一个列表字典,每个列表都需要转换为折线图。但是我对js并没有太多的经验,并且不确定为什么现在页面上什么都没有显示。任何帮助将非常感激

GraphData={
1:[['Col1','Col2'],[Row1,Row1],[Row2,Row2]],
2:[['Col1','Col2'],[Row1,Row1],[Row2,Row2],[Row3,Row3]}
3:[['Col1','Col2','Col3',[Row1,Row1,Row1],[Row2,Row2,Row2]}


#In the <head> area:

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    {% for Vars in GraphData %}
        <script type="text/javascript">
            var Data =GraphData[Vars]
            google.charts.load('current', {'packages':['corechart']});
            google.charts.setOnLoadCallback(drawChart);
            function drawChart() {
            var data = google.visualization.arrayToDataTable(Datas}

            var options = {
                title: 'Graph Data',
                curveType: 'function',
                legend: { position: 'bottom' }
            };
            var chart = new google.visualization.LineChart(document.getElementById(Vars));

            chart.draw(data, options);
            }
        </script>
    {%endfor%}


and in the <body> section:

{% for Vars in GraphData %}
<div id={{Vars}} style="width: 900px; height: 500px"></div>
{% endfor %}



罗科·C·布尔扬
  • 首先将您的数据转换为JSON,并在需要时对其进行解析
  • 创建一个processAllCharts()循环所有数据的函数
  • 将该函数作为回调调用: google.charts.setOnLoadCallback(processAllCharts);
  • 在该循环内,将所有数据发送到 drawChart
  • 循环数据 function drawAllCharts() { GraphData.forEach(drawChart); }
  • 动态创建图表DIV

const GraphData = [
  [
    ['Year', 'Foo', 'Bar'],
    ['2017', 2000, 400],
    ['2018', 660, 1120],
    ['2019', 1030, 540],
  ],
  [
    ['Year', 'Lorem', 'Ipsum'],
    ['2014', 780, 1400],
    ['2015', 998, 770],
    ['2016', 660, 1120],
  ],
  [
    ['Year', 'Dolor', 'Sit Amet'],
    ['2017', 660, 1120],
    ['2018', 1030, 540],
  ],
];

const EL_gAllCharts = document.querySelector("#gAllCharts");
const chartOptions = {
  title: 'Graph Data',
  curveType: 'function',
  legend: {
    position: 'bottom'
  }
};

function drawChart(data) {
  // Create charts
  const EL_gChart = document.createElement("div");
  EL_gChart.classList.add("gChart");
  gAllCharts.append(EL_gChart);

  const gData = google.visualization.arrayToDataTable(data)
  const chart = new google.visualization.LineChart(EL_gChart);
  chart.draw(gData, chartOptions);
}

function processAllCharts() {
  GraphData.forEach(drawChart);
}

google.charts.load('current', {'packages': ['corechart']});
google.charts.setOnLoadCallback(processAllCharts);
.gChart {
  height: 260px;
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="gAllCharts"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过按日期分组从一个表创建多个折线图的系列

如何创建一个在两个轴上都有日期的折线图?

Chart.js-多个折线图-仅显示最后一个图表

如何在一个图中绘制多个折线图(覆盖/分组)

如何在一个折线图中绘制多个组的配对均值?

如何使用ggplot在一个图表中创建条形图和折线图

如何创建一个串联的 altair 折线图,如以下数据框所示的示例?

根据一个或多个下拉选择创建具有不同类型线型的折线图

我如何在R中的折线图中做一个折线或空白

如何制作具有多个系列的折线图,其中一个系列的值为空

如何以绘图方式绘制一个x值的具有多个y值的折线图?

如何编写一个函数来为数据框中的每个因素绘制折线图?

如何在一个JSON中获取两个查询以显示在Chart.js折线图上?

Matplotlib 将多个折线图绘制成一个轴

如何在R中创建多个折线图

每分钟在Excel中创建一个折线图

使用 R 中的 ggplot 为每个国家/地区创建一个折线图

Chart.js,返回一个“日期”数组来绘制折线图

D3.js 在折线图上每隔一个点添加圆圈

D3如何找到折线图中最后一个点的高度

r 中一个变量的折线图,如何着色?

React d3-如何:在一个折线图上有几个战神

如何在折线图中显示系列中的第一个数据?

如何从Excel的一个表中的两个数据集构建折线图

如何将另一个数据集中的折线图添加到已使用构面网格的现有折线图上?

如何使用Matplotlib创建折线图

如何创建多系列折线图

如何使用chartjs创建折线图?

如何在highChart.js折线图中的Y轴上的标签中显示最后一个数据点值