I'm starting with the c3.js library and I want to make a time chart (timeseries) and color-code the values based on some intervals. I receive the data from a json after making a query in PHP. I make an array for the dates and an array for the values, and receive them in the javascript function to make the chart like so:
let xData = (data.date);
let yData = (data.value);
xData.unshift('x');
yData.unshift('Values');
var chart7 = c3.generate({
bindto: '#chart7',
data: {
x:'x',
xFormat: '%Y-%m-%d',
columns:[
xData,
yData
],
type: 'bar'
},
axis: {
x:{
label:{
text:'Date',
position:'outer-center'
},
type: 'timeseries',
tick:{
format: '%Y-%m-%d'
}
},
},
point: {
show: true,
//r:3.5
},
zoom: {
enabled: true
},
legend: {
show: false
},
color: {
pattern: ['#b7b485', '#0E6A40', '#B73540', '#B73540']
},
padding: {
top: 5,
right: 5,
bottom: 5,
left: 30,
},
size:{
height:240,
}
});
the json data would be something like this: (I give an example):
['x', '2022-05-01', '2022-05-02', '2022-05-03', '2022-05-04', '2022-05-05', '2022-05-06', '2022-05-07', '2022-05-08', '2022-05-09', '2022-05-10', '2022-05-12', '2022-05-13', '2022-05-14', '2022-05-15', '2022-05-16', '2022-05-17', '2022-05-18', '2022-05-19', '2022-05-20', '2022-05-21']
['Values', '0.0249', '0.0163', '0.0163', '0.0163', '0.0094', '0.0066', '0.0066', '0.0066', '0.0066', '0.0066', '0.0011', '0.0011', '0.0011', '0.0006', '0.0042', '0.0163', '0.0163', '0.0163', '0.0011', '0.0011']
I want to be able to put them in three colors, green, yellow and red based on an intervals if the value is less than 0.3: green color if the value is greater than 0.3 and less than 0.6: yellow color if the value is greater than 0.6: red color I don't know if this is possible. I thought about creating 6 arrays already in php 3 for dates and 3 for values, but when trying to put them in the graph I was not able to represent them. If I put a single 'x' for a date, it gave me several values and it couldn't be because these values are unique. Does anyone know how I can represent color intervals?
You can use
Here's your example adapted - I used other intervals to fit your example and a more elaborate
colorfunction to control the color outside the plot itself (e.g., in the legend) as the the last one (red).