Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
63 views
in Technique[技术] by (71.8m points)

javascript - ChartJS - how to create chart?

I have a dateset like below:

[
    [
        1446940800, //week in epoch
        705077, //additions
        -279 //delitions
    ],
    [
        1447545600,
        7069,
        -5261
    ],
    [
        1448150400,
        13874,
        -11025
    ],
]

how to create a very simply chart with using chartjs with provided data? I'm totaly newbie with chartjs and i do not know how to start :(

i need to create this in vuejs, thanks a lot for all help

question from:https://stackoverflow.com/questions/65875284/chartjs-how-to-create-chart

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I wrote a function to reformat your data so that it could easily fit in a chart (push the different values in arrays).

const data = [
    [
      1446940800, //week in epoch
      7050, //additions
      -279 //delitions
    ],
    [
      1447545600,
      7069,
      -5261
    ],
    [
      1448150400,
      13874,
      -11025
    ],
  ];

  function reformatdata(datapoints) {
    const reformatted = {
      weeks: [],
      additions: [],
      deletions: [],
    }
    for (let i = 0; i < datapoints.length; i++) {
      reformatted.weeks.push(datapoints[i][0]);
      reformatted.additions.push(datapoints[i][1]);
      reformatted.deletions.push(datapoints[i][2]);
    }
    return reformatted;
  }

  const chartdata = reformatdata(data);
  console.log(chartdata);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...