Reputation: 690
I'm trying to create a fairly simple time series chart for payments made at X time for Y amount. X-axis labels are fixed at every 12 hours like this:
new Date("2018-08-28 00:00"),
new Date("2018-08-28 12:00"),
new Date("2018-08-29 00:00"),
new Date("2018-08-29 12:00")
.............................
When I hard-code array of amount/time I get decent graph but without X-axis labels: https://jsfiddle.net/tolyan/69z2wepo/281812/. When I try to use some real data and map it to array, labels are generated instead of my fixed labels: https://jsfiddle.net/tolyan/69z2wepo/281806/. Also, how do I configure chartist to have some padding or gap for the graph start - for better readability. Thanks in advance for any tips!
Upvotes: 1
Views: 5321
Reputation: 1027
I think there's nothing wrong with your mapping of the real data, I think is more of knowing your data and putting the correct "ticks" in the x axis, for example in your data you have stamps in terms of seconds, for example:
{amount: 22, timestamp: "2018-08-29T06:01:54.007"},
{amount: 1, timestamp: "2018-08-29T06:01:55.29"},
{amount: 11, timestamp: "2018-08-29T06:01:56.66"},
{amount: 9, timestamp: "2018-08-29T06:01:58.063"},
So of course if you are going to have your ticks set from the 28th at 00:00 till the 31st at 23:59, then it won't work and it won't show the labels because the graph needs to create that space in between for it to show the ticks, that's why it shows the dot at the beginning and at the end. So adjusting your example in the fiddle, try to put lower values to your ticks, something like this:
axisX: {
type: Chartist.FixedScaleAxis,
divisor: 4,
ticks: [
new Date("2018-08-29 06:01:52"),
new Date("2018-08-29 06:01:53"),
new Date("2018-08-29 06:01:55"),
new Date("2018-08-29 06:01:59")
],
So the full example should be something like:
var trans = [
{amount: 22, timestamp: "2018-08-29T06:01:54.007"},
{amount: 1, timestamp: "2018-08-29T06:01:55.29"},
{amount: 11, timestamp: "2018-08-29T06:01:56.66"},
{amount: 9, timestamp: "2018-08-29T06:01:58.063"},
{amount: 6, timestamp: "2018-08-29T06:02:02.203"},
{amount: 1, timestamp: "2018-08-29 06:02:03.413"}
];
var data = {
series: [
{
name: 'series-times',
data: trans.map((prop, key) => {
return {
x: new Date(prop["timestamp"]),
y: prop["amount"]
};
})
}
]
};
var options = {
lineSmooth: Chartist.Interpolation.cardinal({
tension: 0
}),
axisY: {
type: Chartist.FixedScaleAxis,
divisor: 8,
ticks: [0, 10, 20, 30, 40, 50, 60],
labelInterpolationFnc: function(value) {
return "$" + value;
}
},
axisX: {
type: Chartist.FixedScaleAxis,
divisor: 4,
ticks: [
new Date("2018-08-29 06:01:52"),
new Date("2018-08-29 06:01:53"),
new Date("2018-08-29 06:01:55"),
new Date("2018-08-29 06:01:59")
],
labelInterpolationFnc: function(value) {
return moment(value).format('MM/DD/YY HH:mm:ss');
}
},
// low: 0,
// high: 100,
showPoint: true,
height: "300px",
animation: {
draw: function(data) {
if (data.type === "line" || data.type === "area") {
data.element.animate({
d: {
begin: 600,
dur: 700,
from: data.path
.clone()
.scale(1, 0)
.translate(0, data.chartRect.height())
.stringify(),
to: data.path.clone().stringify(),
easing: Chartist.Svg.Easing.easeOutQuint
}
});
} else if (data.type === "point") {
data.element.animate({
opacity: {
begin: (data.index + 1) * delays,
dur: durations,
from: 0,
to: 1,
easing: "ease"
}
});
}
}
}
}
/* var options = {
seriesBarDistance: 100
}; */
new Chartist.Line('.ct-chart', data, options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<link href="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.css" rel="stylesheet"/>
<div class="ct-chart ct-square"></div>
<script src="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>
For changing the padding of the labels, just target them with CSS, you can just check them out which is the label class and adjust it to your needs. Or also another more cleaner solution is to change the JavaScript options of the tick, or the labels, more info in: https://gionkunz.github.io/chartist-js/api-documentation.html
Hope it helps, let me know if you have questions. Leo.
Upvotes: 2