Angular - 动态构建JSON以与chartist

时间:2016-11-21 23:31:19

标签: javascript angularjs json

我有一个离子项目,并使用以下库: http://gionkunz.github.io/chartist-js/index.html

实际上,使用以下内容绘制图表:

var chart = new Chartist.Line('.ct-chart', {
  series: [
{
  name: 'series-1',
  data: [
    {x: new Date(143134652600), y: 53},
    {x: new Date(143234652600), y: 40},
    {x: new Date(143340052600), y: 45},
    {x: new Date(143366652600), y: 40},
    {x: new Date(143410652600), y: 20},
    {x: new Date(143508652600), y: 32},
    {x: new Date(143569652600), y: 18},
    {x: new Date(143579652600), y: 11}
  ]
},
{
  name: 'series-2',
  data: [
    {x: new Date(143134652600), y: 53},
    {x: new Date(143234652600), y: 35},
    {x: new Date(143334652600), y: 30},
    {x: new Date(143384652600), y: 30},
    {x: new Date(143568652600), y: 10}
  ]
}
 ]
  }, {
axisX: {
type: Chartist.FixedScaleAxis,
divisor: 5,
labelInterpolationFnc: function(value) {
  return moment(value).format('MMM D');
}
}
});

使用DIV:

  <div class="ct-chart ct-perfect-fourth"></div>

如上所示,我没有为该系列提供硬编码数组,而是希望通过函数调用动态构建它。

我如何做到这一点的任何例子?

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以使用生成函数生成具有一点随机性和一些固定变量的数据。为了便于以后重新创建,可能还可以更好地参数化图表的创建。 Chartist还有一个update()函数,可以让你提供新的数据和选项,因此对此特别有用。

<强> JSFIDDLE

<强>的Javascript

var button = document.getElementById('button');
var options = {
    axisX: {
        type: Chartist.FixedScaleAxis,
        divisor: 5,
        labelInterpolationFnc: function(value) {
          return moment(value).format('MMM D');
        }
    }
};
var chart; // initialise the chart variable

function createChart(){
    chart = new Chartist.Line('.ct-chart', changeData(), options);
}

function updateChart(){
    chart.update(changeData());
}

function changeData(){
    var series = [];
    // set up series ranges
    var numberOfSeries = 2;
    var numberOfItems = 8;
    var startDate = 143134652600;
    var endDate = 143579652600;
    var minY = 11;
    var maxY = 53;
    // creates the 'step' each x-axis item should take
    var dateDiff = Math.floor((endDate - startDate) / numberOfItems);
    // alternatively set the step to a whole day etc. (makes endDate unnecessary)
    // var dateDiff = 24 * 60 * 60 * 1000;

    for(var x = 0; x < numberOfSeries; x++){
        var seriesData = [];
        for(var y = 0; y < numberOfItems; y++){
            seriesData.push({
                x: new Date(startDate + (dateDiff*y)),
                y: getRandomInt(minY, maxY)
            })
        }
        series.push({
            name: 'series-'+ (x+1),
            data: seriesData
        });
    }
    // return the data to display in the chart
    return {series:series};
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive)
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

button.addEventListener('click', updateChart);

createChart(); // generate chart initially

<强> HTML

<button id="button">
Change Data
</button>
<div class="ct-chart ct-perfect-fourth"></div>

答案 1 :(得分:1)

这是一个粗略的例子;你可以用数组数组替换输入series1series2,并使较低的for循环两个包装循环来处理多个系列。这还需要将对象添加到外部循环中的系列数组中。

现在,尝试这样的事情:

function generateJSON(series1, series2) {
  var chartInternal = {
    series: [
      {
      name: 'series-1',
      data: []
      },
      {
      name: 'series-2',
      data: []
      }
      ]
    }, {
      axisX: {
        type: Chartist.FixedScaleAxis,
        divisor: 5,
        labelInterpolationFnc: function(value) {
          return moment(value).format('MMM D');
        }
      }
    };

  for (var i = 0, len = series1.length; i < len; i++) {
    chartInternal.series[0].data.push({x: new Date(series1[i].date), y: series1[i].y});
  }
  for (var i = 0, len = series2.length; i < len; i++) {
    chartInternal.series[1].data.push({x: new Date(series2[i].date), y: series2[i].y});
  }
  return chartInternal;
}

用法:

var series1 = [
  { date: 1234567, y:52 },
  ... more
];
var series2 = [
  { date: 7654321, y:52 },
  ... more
];
var chart = new Chartist.Line('.ct-chart', generateJSON(series1, series2))