简体   繁体   English

如何在一页中使用多个折线图

[英]How to use multiple line charts in one page

I have a line chart where the values are from DB, and it plots well.我有一个折线图,其中的值来自 DB,并且绘制得很好。

Here's the code Chart #1:这是代码图表#1:

<?php
    $dataPoints = array( 
        array("y" => $todayv1, "label" => $todayh1),
        array("y" => $todayv2, "label" => $todayh2),
        array("y" => $todayv3, "label" => $todayh3),
        array("y" => $todayv4, "label" => $todayh4),
        array("y" => $todayv5, "label" => $todayh5)
    );
    $dataPoints2 = array(
       array("y" => $yv1, "label" => $yh1),
       array("y" => $yv2, "label" => $yh2),
       array("y" => $yv3, "label" => $yh3),
       array("y" => $yv4, "label" => $yh4),
       array("y" => $yv5, "label" => $yh5)
   );
?>   

<script>
     window.onload = function () {

     var chart = new CanvasJS.Chart("chartContainer", {
         title: {
              text: ""
                },
         axisY: {
              title: ""
                },
         data: [{
              type: "line",
              name: "Today",
              showInLegend: true,
              visible: true,
              dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>
              },
              {
              type: "line",
              lineDashType: "dash",  
              name: "Yesterday",
              showInLegend: true,
              visible: true,
              dataPoints: <?php echo json_encode($dataPoints2, JSON_NUMERIC_CHECK); ?>
             }]
           });
             chart.render();
       }
</script>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<div id="chartContainer" style="height: 370px; width: 100%;"></div>

And I tried to replicate the same graph with same values in the same page.我试图在同一页面中复制具有相同值的相同图表。

But the first graph is not being plotted when I add the code for second graph.但是当我为第二张图添加代码时,没有绘制第一张图。 I even changed the variable names in JS to prevent over writing values, yet the same.我什至更改了 JS 中的变量名称以防止过度写入值,但还是一样。 Only one graph is being plotted in one page.一页中只绘制了一张图表。

Here's the code for second graph Chart #2:这是第二张图表 #2 的代码:

<?php
    $dataPointsy = array( 
       array("y" => $todayv1, "label" => $todayh1),
       array("y" => $todayv2, "label" => $todayh2),
       array("y" => $todayv3, "label" => $todayh3),
       array("y" => $todayv4, "label" => $todayh4),
       array("y" => $todayv5, "label" => $todayh5)
    );
    $dataPointsy2 = array(
       array("y" => $yv1, "label" => $yh1),
       array("y" => $yv2, "label" => $yh2),
       array("y" => $yv3, "label" => $yh3),
       array("y" => $yv4, "label" => $yh4),
       array("y" => $yv5, "label" => $yh5)
    );
?>   

<script>
     window.onload = function () {

     var charty = new CanvasJS.Chart("chartContainery", {
         title: {
             text: ""
                },
         axisY: {
             title: ""
                },
         data: [{
             type: "line",
             name: "Today",
             showInLegend: true,
             visible: true,
             dataPoints: <?php echo json_encode($dataPointsy, JSON_NUMERIC_CHECK); ?>
           },
           {
           type: "line",
           lineDashType: "dash",  
           name: "Yesterday",
           showInLegend: true,
           visible: true,
           dataPoints: <?php echo json_encode($dataPointsy2, JSON_NUMERIC_CHECK); ?>
         }]
       });
       charty.render();
    }
</script>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<div id="chartContainery" style="height: 370px; width: 100%;"></div>

Any Help is greatly appreciated..任何帮助是极大的赞赏..

You don't need to include canvasjs js file multiple time on same page.您不需要在同一页面上多次包含canvasjs js 文件。 You can include it once and use it like:您可以包含一次并像这样使用它:

 var chart1 = new CanvasJS.Chart("chartContainer",{ title:{ text: "Live Data" }, data: [{ type: "line", dataPoints: [ { label: "apple", y: 10 }, { label: "orange", y: 15 }, { label: "banana", y: 25 }, { label: "mango", y: 30 }, { label: "grape", y: 28 } ] }] }); var chart2 = new CanvasJS.Chart("chartContainery",{ title:{ text: "Live Data" }, data: [{ type: "column", dataPoints: [ { label: "apple", y: 10 }, { label: "orange", y: 15 }, { label: "banana", y: 25 }, { label: "mango", y: 30 }, { label: "grape", y: 28 } ] }] }); chart1.render(); chart2.render();
 <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script> <div id="chartContainer" style="height: 370px; width: 100%;"></div> <div id="chartContainery" style="height: 370px; width: 100%;"></div>


Or, if you want to display the charts side-by-side you can also do it like:或者,如果您想并排显示图表,您也可以这样做:

 var chart1 = new CanvasJS.Chart("chartContainer",{ title:{ text: "Live Data" }, data: [{ type: "line", dataPoints: [ { label: "apple", y: 10 }, { label: "orange", y: 15 }, { label: "banana", y: 25 }, { label: "mango", y: 30 }, { label: "grape", y: 28 } ] }] }); var chart2 = new CanvasJS.Chart("chartContainery",{ title:{ text: "Live Data" }, data: [{ type: "column", dataPoints: [ { label: "apple", y: 10 }, { label: "orange", y: 15 }, { label: "banana", y: 25 }, { label: "mango", y: 30 }, { label: "grape", y: 28 } ] }] }); chart1.render(); chart2.render();
 <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script> <div id="chartContainer" style="width: 45%; height: 370px; display: inline-block"></div> <div id="chartContainery" style="width: 45%; height: 370px; display: inline-block"></div>


So, update your code like:因此,更新您的代码,如:

<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<div id="chartContainer" style="height: 370px; width: 100%;"></div>
<div id="chartContainery" style="height: 370px; width: 100%;"></div>

<script>
   // All the chart related js code here
</script>

For more info:欲了解更多信息:

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

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