Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly line chart - multiple lines

I want to create a line chart with three lines (male,female,unknown). This is a sample of my data:

timestamp;sex;number
06:00;male;5
07:00;male;2
07:00;unkown;3
07:00;female;4
09:00;female;4

Is there a option in plotly to automatically create the three lines or do I need to loop through the data and create three traces by myself? This is my code so far:

var trace1 = {
  type: 'scatter',
  x: x,
  y: y,
  name: "Male",
  transforms: [{
    type: 'aggregate',
    groups: x,
    aggregations: [
      {target: 'y', func: 'count', enabled: true},
    ]
  }]
};


var data = [trace1];

Plotly.newPlot('myDiv', data, {title: 'Plotting CSV data from AJAX call'});
like image 395
ZerOne Avatar asked Nov 24 '25 09:11

ZerOne


1 Answers

You need to create different data set(trace) for each category.
Maybe this can help you.

var men = {
  x: x, 
  y: y, 
  type: 'scatter',
  name:'male'
};

var female = {
  x: x, 
  y: y, 
  type: 'scatter',
  'name':'female'
};
var unknown = {
  x: x, 
  y:y, 
  type: 'scatter',
  'name':'unknown'
};

var data = [men, female,unknown];
like image 82
Md Nasir Fardoush Avatar answered Nov 25 '25 22:11

Md Nasir Fardoush