简体   繁体   English

如何在Chart.js中绘制日期

[英]How to plot dates in Chart.js

I am trying to make a chart for my website that takes a year's worth of dates (entries for each day), and then plots it on the graph. 我正在尝试为我的网站制作一张图表,该图表需要一年的日期(每天的条目),然后在图表上绘制。

I tried writing a function that makes a list of all dates in that v=certain range in the format of 我尝试编写一个函数,该函数列出了v =某个范围内所有日期的格式

"date1", "date2", "date3", ...

and then places that variable string into the series of chart.js 然后将变量字符串放入chart.js系列中

var lineChartData = {
    labels: ["January", "February", "March", "April", "May", "June", "July", "August"],
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.2)",
            strokeColor: "rgba(220,220,220,1)",
            pointColor: "rgba(220,220,220,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(220,220,220,1)",
            data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
        },
        {
            label: "My Second dataset",
            fillColor: "rgba(151,187,205,0.2)",
            strokeColor: "rgba(151,187,205,1)",
            pointColor: "rgba(151,187,205,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(151,187,205,1)",
            data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
        }
    ]

When I have it like code above, it will produce the graph. 当我像上面的代码一样,它将生成图形。 But I need it to be dynamic, so I have functions that produce the dates and data. 但我需要它是动态的,所以我有生成日期和数据的函数。 But when calling those functions into the variable linechartData, the chart does not work. 但是当将这些函数调用到变量linechartData中时,图表不起作用。

var lineChartData = {
    labels: [genDates()],
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.2)",
            strokeColor: "rgba(220,220,220,1)",
            pointColor: "rgba(220,220,220,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(220,220,220,1)",
            data: [genData()]
        },

This code directly above will not work. 上面的代码不起作用。

If any one has any suggestions on how to plot the data, please post below. 如果任何人对如何绘制数据有任何建议,请在下面发布。

The genData method just generates random numbers genData方法只生成随机数

 function gendata() {
     var i = 0;
     var data = "";
     while (i < 365) {
         data = data + Math.round(Math.random() * 100) + ", ";
         i = i + 1;
     }
     return data;
 }

gendate method gendate方法

function getDates(startDate, stopDate) {
    var dateArray = new Array();
    var currentDate = startDate;
    while (currentDate <= stopDate) {
        dateArray.push(new Date(currentDate));
        currentDate = currentDate.addDays(1);
    }
    dates = "";
    for (i = 0; i < dateArray.length; i++) {
        date = date + dateArray[i] + ", ";
    }
    return dates;
}

The problem is that there's a type mismatch at genDates . 问题是genDates的类型不匹配。 The labels array will evaluate as an array with a single string in it unlike in your original example. 与原始示例不同,labels数组将评估为包含单个字符串的数组。 To work around this you could return an array directly and remove the wrapping array from your chart definition. 要解决此问题,您可以直接返回一个数组,并从图表定义中删除包装数组。

var areaChartData = {
labels: labelsnuevos(productos),
   // here goes my dataset
};

function labelsnuevos(productos)
{
    var labels = [];

    $.each(productos, function(key, value) {
        var lab = value.nombre;
        labels.push(lab);
    });
    return labels;
}

I have productos instead of dates, but you need to pass your "dates" to a variable, in this case my variable I sproductos, then with foreach you take and push its value. 我有产品而不是日期,但你需要将你的“日期”传递给一个变量,在这种情况下我的变量是我的sproductos,然后用foreach你接受并推送它的值。

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

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