将此 canvasjs 图表转换为 chart.js 图表

Convert this canvasjs chart to chart.js chart

我是 javascript 编程的新手,还在学习,几天后我创建了这个 canvasjs 图表,它很有效,

但最近我注意到 canvasjs 不是免费的,它会在图表下方创建水印。

我发现 chart.js 哪个免费(我认为)和图表样式更漂亮

有人可以帮我把这个 canvasjs 图表转换成 chart.js 图表吗?

$(function () {
    var DATA1 = []
    var DATA2 = []

    $.ajax({
        type : 'GET',
        url : "https://api.myjson.com/bins/14edgx",
        dataType : 'json',
        success : function (field) {
            field = field.data;
            for (var i = 0; i < field.length; i++) {
                DATA1.push({
                    x : new Date(field[i].day),
                    y : parseInt(field[i].money)
                });
                DATA2.push({
                    x : new Date(field[i].day),
                    y : parseInt(field[i].impressions)
                });
            }
            var chart = new CanvasJS.Chart("chartContainer", {
                    animationEnabled : true,
                    backgroundColor : "#FFF",
                    title : {},
                    axisY : [{
                            title : "Impression",
                            lineColor : "#369EAD",

                        }
                    ],
                    axisY2 : [{
                            title : "Money",
                            lineColor : "#C0504E",
                            valueFormatString : "$#,###,#0",

                        }
                    ],
                    axisX : {
                        valueFormatString : "DD-MMM",
                        labelAngle : -50,
                        interlacedColor : "#F0F8FF"
                    },
                    data : [{
                            type : "splineArea",
                            fillOpacity : .9,
                            color : "#C0504E",
                            axisYType : "primary",
                            axisYIndex : 0,
                            name : "line1",
                            toolTipContent : "{x}<br/>Views: {y}",

                            showInLegend : true,
                            legendText : "Impression",
                            dataPoints : DATA2

                        }, {
                            type : "splineArea",
                            fillOpacity : .2,
                            color : "#6599FF",
                            axisYType : "secondary",
                            axisYIndex : 1,
                            name : "Visits",
                            toolTipContent : "{x}<br/>Money: ${y}",
                            showInLegend : true,
                            legendText : "Money",
                            dataPoints : DATA1
                        }]
                });

            chart.render();

        }
    });
});

谢谢

这是从 canvas.js 到 chart.js 的翻译:

var DATA1 = [];
var DATA2 = [];

$.ajax({
  type: 'GET',
  url: "https://api.myjson.com/bins/14edgx",
  dataType: 'json',
  success: function(field) {
    field = field.data;
    for (var i = 0; i < field.length; i++) {

      DATA1.push({
        x: new Date(field[i].day),
        y: parseInt(field[i].money)
      });
      DATA2.push({
        x: new Date(field[i].day),
        y: parseInt(field[i].impressions)
      });
    }
    createChart();
    console.log(DATA1, DATA2);
  }
});

function createChart() {
  var ctx = document.getElementById("myChart").getContext("2d");
  ctx.canvas.width = 1000;
  ctx.canvas.height = 600;
  var cfg = {
    type: 'bar',
    data: {
      datasets: [{
        label: "DATA1",
        data: DATA1,
        type: 'line',
        pointRadius: 1,
        fill: true,
        borderColor: 'red',
        lineTension: 0,
        borderWidth: 2,
        yAxisID: 'A',
      },
      {
        label: "DATA2",
        data: DATA2,
        type: 'line',
        pointRadius: 1,
        fill: true,
        borderColor: 'blue',
        lineTension: 0,
        borderWidth: 2,
        yAxisID: 'B',
      }]
    },
    options: {
      legend: {
        display: false
      },
      tooltips: {
        mode: 'nearest',
        intersect: false,
        callbacks: {
          title: function(tooltipItem, data) {
            console.log('title',tooltipItem, data);
            var date = moment(tooltipItem[0]['xLabel']).format('DD.MM.YYYY');
            return date;
          },
          label: function(tooltipItem, data) {
            console.log('label',tooltipItem, data);
            return data.datasets[0].label+": "+tooltipItem['yLabel'];
          }
        },
      },
      scales: {
        xAxes: [{
          type: 'time',
          distribution: 'linear',
          time: {
            unit: 'day',
            unitStepSize: 1,
            displayFormats: {
              'day': 'DD-MM-YY'
            }
          }
        }],
        yAxes: [{
          id: 'A',
   scaleLabel: {
            display: true,
            labelString: 'money'
          },
          position: 'left',
        }, {
          id: 'B',
          type: 'linear',
   scaleLabel: {
            display: true,
            labelString: 'impressions'
          },
          position: 'right',
        }]
      }
    }
  };
  var chart = new Chart(ctx, cfg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>

<canvas id="myChart" width="400" height="200"></canvas>