zoukankan      html  css  js  c++  java
  • cahrts学习

    最近公司项目要用到chart.js,故开始学习。以前从未接触过,本人学前端3个月左右。也是刚听说过echart。还不知道chart和echart的差别。我想总有机会把这两插件都了解的。那些了解charts。

    先来个中文的api地址:http://www.bootcss.com/p/chart.js/docs/。

    不过我总觉得中文的不太齐全,也可以看看英文的,就当学英语了:http://chartjs.cn/docs/#getting-started-include-chart.js

    先把API粗略的浏览了一遍,内容相当少。接下去可以自己动手试试了。

    一、创建图标根据API先HTML部分,记得canvas外面一定要套一个div,用于规定图表大小。

    <div style="height: 400px;  400px; overflow: hidden;">
    
    <canvas id="chart" ></canvas>
    </div>

    二、导入js,如果用jQuery创建,别忘了导入jQuery

    <script type="text/javascript" src="js/jquery-2.2.3.min.js</script>
    <script type="text/javascript" src="js/Chart.js" ></script>

     三、创建图表,这里以双曲线为例

     var areaChart1Canvas = $("#chart").get(0).getContext("2d");
        // This will get the first returned node in the jQuery collection.
        var areaChart1 = new Chart(areaChart1Canvas);
        var areaChart1Data = {
          labels: ["生产一部", "生产二部", "市场一部", "市场二部", "服务部", "后勤部", "园区过道"],
          datasets: [
            {
              label: "Electronics",
              fillColor: "rgba(210, 214, 222, 1)",
              strokeColor: "rgba(210, 214, 222, 1)",
              pointColor: "rgba(210, 214, 222, 1)",
              pointStrokeColor: "#c1c7d1",
              pointHighlightFill: "red",
              pointHighlightStroke: "red",
              data: [65, 59, 80, 81, 56, 55, 40]
            },
            {
              label: "Digital Goods",
              fillColor: "rgba(60,141,188,0.9)",
              strokeColor: "rgba(60,141,188,0.8)",
              pointColor: "#3b8bba",
              pointStrokeColor: "rgba(60,141,188,1)",
              pointHighlightFill: "#fff",
              pointHighlightStroke: "rgba(60,141,188,1)",
              data: []//也可以通过外面push进去
            }
          ]
        };
        var arry=[28, 48, 40, 19, 86, 27, 90];
        for(var i=0;i<arry.length;i++){
            areaChart1Data.datasets[1].data.push(arry[i]);//将数组写进data
        }
    
        var areaChart1Options = {
          //Boolean - If we should show the scale at all
          showScale: true,
          //Boolean - Whether grid lines are shown across the chart
          scaleShowGridLines: false,
          //String - Colour of the grid lines
          scaleGridLineColor: "rgba(0,0,0,.05)",
          //Number - Width of the grid lines
          scaleGridLineWidth: 1,
          //Boolean - Whether to show horizontal lines (except X axis)
          scaleShowHorizontalLines: true,
          //Boolean - Whether to show vertical lines (except Y axis)
          scaleShowVerticalLines: true,
          //Boolean - Whether the line is curved between points
          bezierCurve: true,
          //Number - Tension of the bezier curve between points
          bezierCurveTension: 0.3,
          //Boolean - Whether to show a dot for each point
          pointDot: false,
          //Number - Radius of each point dot in pixels
          pointDotRadius: 4,
          //Number - Pixel width of point dot stroke
          pointDotStrokeWidth: 1,
          //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
          pointHitDetectionRadius: 20,
          //Boolean - Whether to show a stroke for datasets
          datasetStroke: true,
          //Number - Pixel width of dataset stroke
          datasetStrokeWidth: 2,
          //Boolean - Whether to fill the dataset with a color
          datasetFill: true,
          //String - A legend template
          legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].lineColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>",
          //Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
          maintainAspectRatio: true,
          //Boolean - whether to make the chart responsive to window resizing
          responsive: true
        };
    
        //Create the line chart
        areaChart1.Line(areaChart1Data, areaChart1Options);

    到这里一定要注意各种插件以及canvas的位置。canvas要在charts插件前面,后门具体创建图表的代码要在插件后面。总之,按我这里的顺序来是准没错的。

    四、到目前为止,一个双曲线图就创建好了。但是如果我们要建另一个图,却用到上面曲线图的数据怎么办,这里可以直接调用,并且修改个别参数。代码如下。

        var barChart1Canvas = $("#barChart1").get(0).getContext("2d");
        var barChart1 = new Chart(barChart1Canvas);
        var barChart1Data = areaChart1Data;
        barChart1Data.datasets[1].fillColor = "#0073b7";
        barChart1Data.datasets[1].strokeColor = "#0073b7";
        barChart1Data.datasets[1].pointColor = "#0073b7";
        var barChart1Options = {
          //Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
          scaleBeginAtZero: true,
          //Boolean - Whether grid lines are shown across the chart
          scaleShowGridLines: true,
          //String - Colour of the grid lines
          scaleGridLineColor: "rgba(0,0,0,.05)",
          //Number - Width of the grid lines
          scaleGridLineWidth: 0,
          //Boolean - Whether to show horizontal lines (except X axis)
          scaleShowHorizontalLines: true,
          //Boolean - Whether to show vertical lines (except Y axis)
          scaleShowVerticalLines: true,
          //Boolean - If there is a stroke on each bar
          barShowStroke: true,
          //Number - Pixel width of the bar stroke
          barStrokeWidth: 2,
          //Number - Spacing between each of the X value sets
          barValueSpacing:4,
          //Number - Spacing between data sets within X values
          barDatasetSpacing:0,
          //String - A legend template
          legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].fillColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>",
          //Boolean - whether to make the chart responsive
          responsive: true,
          maintainAspectRatio: true
        };
    
        barChart1Options.datasetFill = false;
        barChart1.Bar(barChart1Data, barChart1Options);

    至于具体参数的含义,这里有英文的注释,就不翻译了。实在不理解可以把这里的代码复制过去,然后一一尝试。

  • 相关阅读:
    request:fail parameter error: parameter.url should be String instead of Undefined;
    高性能Mysql笔记 — 索引
    机器学习 — 构建价格模型
    机器学习 — 决策树建模
    机器学习 — 文档过滤
    机器学习 — 优化
    机器学习 — 搜索及排名
    机器学习 — 发现群组
    机器学习 — 提供推荐
    docker
  • 原文地址:https://www.cnblogs.com/xwtcm/p/7263639.html
Copyright © 2011-2022 走看看