why my lineChart show the month in the xAxis instead of sunday?
Thanks for help =)
Here is my code:
var hitslineChart = dc.lineChart("#chart-line-hitsperday");
var data = [
{date: "12/27/2012", http_404: 2, http_200: 190, http_302: 100},
{date: "12/28/2012", http_404: 2, http_200: 10, http_302: 100},
{date: "12/29/2012", http_404: 1, http_200: 300, http_302: 200},
{date: "12/30/2012", http_404: 2, http_200: 90, http_302: 0},
{date: "12/31/2012", http_404: 2, http_200: 90, http_302: 0},
{date: "01/01/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/02/2013", http_404: 1, http_200: 10, http_302: 1},
{date: "01/03/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/04/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/05/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/06/2013", http_404: 2, http_200: 200, http_302: 1},
{date: "01/07/2013", http_404: 1, http_200: 200, http_302: 100}
];
var ndx = crossfilter(data);
var parseDate = d3.time.format("%m/%d/%Y").parse;
data.forEach(function(d) {
d.date = Date.parse(d.date);
d.total= d.http_404+d.http_200+d.http_302;
});
var dateDim = ndx.dimension(function(d) {return d.date;});
var hits = dateDim.group().reduceSum(function(d) {return d.total;});
var minDate = dateDim.bottom(1)[0].date;
var maxDate = dateDim.top(1)[0].date;
hitslineChart
.width(500).height(200)
.dimension(dateDim)
.group(hits)
.x(d3.time.scale().domain([minDate,maxDate]))
.xAxis().ticks(5)
.yAxisLabel("Hits per day");
dc.renderAll();![the result][1]
the result on the xAxis is : Thu27-Fri28-Sat29-Dec30-Mon31-2013-Wed02-Thu03-Fri04-Sat05-Jan06...
1) You will want to parse your incoming data using the date formatter:
d.date = Date.parse(d.date); \\ Old
d.date = parseDate.parse(d.date); \\ New
This will explicitly match the format of your input data.
2) If you want the data to be grouped by days, you will need to specify that in your dimension. The d3.time.day helper method will set the time to 00:00:00 on the data supplied. This makes the data group together like you want:
var dateDim = ndx.dimension(function(d) {return d.date;}); \\old
var dateDim = ndx.dimension(function(d) {return d3.time.day(d.date);}); \\ new
It's not clear whether you want a point per day (Jan 1, Jan 2, Jan 3, etc) or a point per day of the week (Sun, Mon, Tues). The d3.time.day method groups the date by the calendar day.
3) You can specify the tick format using the following method:
.xAxis().tickFormat(function(v) {return displayDate(v)});
Details about time formatting is available at https://github.com/mbostock/d3/wiki/Time-Formatting.
Here is a jsfiddle that adapts your code to display day of week on the x-axis:
http://jsfiddle.net/djmartin_umich/6V4Ey/
I hope this helps!
-DJ
Related
I'm struggling to get a dc.js plot to work. The data object contains 2 value fields, and I need to designate val to scatterPlot and smooth to lineChart. Example below and jsfiddle here.
<div id="series-chart"></div>
<style>div{ display: inline; }</style>
<script>
var datum = [
{date: "2018-01-01T00:00:00Z", val: 0, smooth: 3, type: "set1"},
{date: "2018-01-02T00:00:00Z", val: 7, smooth: 4, type: "set1"},
{date: "2018-01-03T00:00:00Z", val: 1, smooth: 6, type: "set1"},
{date: "2018-01-04T00:00:00Z", val: 4, smooth: 8, type: "set1"},
{date: "2018-01-05T00:00:00Z", val: 8, smooth: 11, type: "set1"},
{date: "2018-01-06T00:00:00Z", val: 15, smooth: 14, type: "set1"},
{date: "2018-01-07T00:00:00Z", val: 12, smooth: 17, type: "set1"},
{date: "2018-01-08T00:00:00Z", val: 20, smooth: 20, type: "set1"},
{date: "2018-01-01T00:00:00Z", val: 15, smooth: 12, type: "set2"},
{date: "2018-01-02T00:00:00Z", val: 10, smooth: 11, type: "set2"},
{date: "2018-01-03T00:00:00Z", val: 14, smooth: 9, type: "set2"},
{date: "2018-01-04T00:00:00Z", val: 7, smooth: 7, type: "set2"},
{date: "2018-01-05T00:00:00Z", val: 12, smooth: 5, type: "set2"},
{date: "2018-01-06T00:00:00Z", val: 8, smooth: 4, type: "set2"},
{date: "2018-01-07T00:00:00Z", val: 8, smooth: 3, type: "set2"},
{date: "2018-01-08T00:00:00Z", val: 5, smooth: 2, type: "set2"}
];
var data = crossfilter(datum);
var typeSeriesDimension = data.dimension(function(d){ return [d.type, new Date(d.date)]; });
var totalGroupRaw = typeSeriesDimension.group().reduceSum(function(d){ return d.val; });
var totalGroupSmooth = typeSeriesDimension.group().reduceSum(function(d){ return d.smooth; });
var composite = dc.compositeChart("#series-chart");
composite
.width(400)
.height(400)
.x(d3.time.scale().domain([new Date("2018-01-01T00:00:00Z"), new Date("2018-01-08T00:00:00Z")]))
.dimension(typeSeriesDimension)
.compose([
dc.lineChart(composite)
.dimension(typeSeriesDimension)
.group(totalGroupSmooth)
// .seriesAccessor(function(d) {return d.key[0];})
.keyAccessor(function(d) {return d.key[1];})
.valueAccessor(function(d) {return d.value;}) ,
dc.scatterPlot(composite)
.dimension(typeSeriesDimension)
.group(totalGroupRaw)
// .seriesAccessor(function(d) {return d.key[0];})
.keyAccessor(function(d) {return d.key[1];})
.valueAccessor(function(d) {return d.value;})
])
.legend(dc.legend().x(100).y(10).itemHeight(20).gap(5));
dc.renderAll();
</script>
The categorical colour mapping doesn't work without seriesAccessor, and uncommented these lines cause script to fail with:
Uncaught TypeError: dc.lineChart(...).dimension(...).group(...).seriesAccessor is not a function
Also the lines are for some reason I can't establish plotting in the wrong order.
All round fail. Any assistance much appreciated!
That dimension makes sense for the scatter plot, but it doesn't make sense for the line chart.
A line chart expects to have simple scalar keys like dates. But the scatter plot expects to have 2D keys like [type, Date].
Luckily the composite chart doesn't really mind if its child charts use different dimensions.
So define a new time dimension and use that for the line chart's group:
var timeSeriesDimension = data.dimension(function(d) { return new Date(d.date);})
var totalGroupSmooth = timeSeriesDimension.group().reduceSum(function(d){ return d.smooth; });
And drop your key and value accessors from the line chart, because the default behavior is fine now:
//.keyAccessor(function(d) {return d.key[1];})
//.valueAccessor(function(d) {return d.value;}) ,
As for seriesAccessor, that's only used with dc.seriesChart, so I'm afraid it won't help you here. It's probably possible to combine a scatter plot with a series chart, because the series chart is just a specialization of the composite chart, but I haven't tried that - and I'd rather leave that for another question if you want to try that out.
I observed that while providing duplicate ticklabels (with an aim to mention the class of categorical variable), plotly creates a unique set and displays only the non-repeated tick labels. Is there a way to by-pass this feature and allow duplicate tick-labels?
var data = [
{
z: [[1, 20, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, -10, 20]],
x: ['Healthy', 'Healthy', 'Moderate', 'Diseased', 'Diseased'],
y: ['Morning', 'Afternoon', 'Evening'],
type: 'heatmap'
}
];
Plotly.newPlot('myDiv', data);
Following is the jsfiddle depicting the issue:
https://jsfiddle.net/mam8sgwx/
Set the layout with ticktext:
var layout = {
xaxis: {
tickmode: "array",
ticktext: ['Healthy', 'Healthy', 'Moderate', 'Diseased', 'Diseased'],
tickvals: [0, 1, 2, 3, 4]
}
}
Here is the demo:
var data = [{
z: [
[1, 20, 30, 50, 1],
[20, 1, 60, 80, 30],
[30, 60, 1, -10, 20]
],
y: ['Morning', 'Afternoon', 'Evening'],
type: 'heatmap'
}];
var layout = {
xaxis: {
tickmode: "array",
ticktext: ['Healthy', 'Healthy', 'Moderate', 'Diseased', 'Diseased'],
tickvals: [0, 1, 2, 3, 4]
}
}
Plotly.newPlot('myDiv', data, layout);
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<body>
<div id="myDiv"></div>
Here is my dataset
var dataset = [{ x: 0, y: 100 }, { x: 1, y: 833 }, { x: 2, y: 1312 },
{ x: 3, y: 1222 }, { x: 4, y: 1611 },
{ x: 0, y: 200 }, { x: 1, y: 933 }, { x: 2, y: 1412 },
{ x: 3, y: 1322 }, { x: 4, y: 1711 },]
I'm not sure where to specify the name of each dataset, How should this be modified to allow for a Group Name to be passed and what changes should be made in my D3.JS code ?
Fiddle https://jsfiddle.net/qvrL3ey5/
Using http://jsfiddle.net/2N2rt/15/ I was able to resolve this issue.
Example
var data = [
{name: 'John', values: [0,1,3,9, 8, 7]},
{name: 'Harry', values: [0, -100, 7, 1, 1, 111]},
{name: 'Steve', values: [3, 1, 4, 4, 4, 107]},
{name: 'Adam', values: [4, 77, 2, 13, 11, 130]}
];
As I keep adding dimensions to my data set, I encounter an issue of having my bar chart not displaying values of my "account" dimension.
Here is my current fiddle: https://jsfiddle.net/eugene_goldberg/yv3nnred/18/
var data = [
{date: "2015-03-25T12:10:00", resolver: "Group 1", escalation_pct: 20, bubble: 5,account: "Aon", region: "Americas", x: 1},
{date: "2015-03-25T12:15:00", resolver: "Group 2", escalation_pct: 10, bubble: 5,account: "Boeing", region: "AMEA", x: 1},
{date: "2015-03-25T12:25:00", resolver: "Group 3", escalation_pct: 50, bubble: 10,account: "Zurich", region: "Nordics", x: 1},
{date: "2015-03-25T12:40:00", resolver: "Group 4", escalation_pct: 30, bubble: 1,account: "Aon", region: "Americas", x: 1},
{date: "2015-03-25T12:35:00", resolver: "Group 5", escalation_pct: 5, bubble: 10,account: "Boeing", region: "Australia", x: 1},
{date: "2015-03-25T12:45:00", resolver: "Group 6", escalation_pct: 13, bubble: 1,account: "Zurich", region: "UK&I", x: 1}
];
var dimAccount = ndx.dimension(function(d) {return + d.account;});
var accountGroup = dimAccount.group().reduceSum(function(d) {return d.x});
var accountChart = dc.barChart("#account-chart");
accountChart.width(480)
.height(150)
.dimension(dimAccount)
.group(accountGroup)
.x(d3.scale.linear().domain([0, 10]))
.elasticY(true)
.xAxis().tickFormat();
It must be something very basic, I'm just not grasping it yet...
In my case, going with a row chart was what I needed.
A fully working fiddle is below:
https://jsfiddle.net/eugene_goldberg/yv3nnred/25/
var dimAccount = ndx.dimension(function(d) {return d.account;});
var accountGroup = dimAccount.group().reduceSum(function (d) {
return d.x;
});
var accountChart = dc.rowChart("#account-chart");
accountChart
.width(500)
.height(500)
.dimension(dimAccount)
.group(accountGroup);
A row chart works good. But another option is a selectmenu, because the height of the bar doesnt represent anything you could have a simple dropdown menu with the options.
(dc.selectMenu on 2.1 branch)
Fiddle: http://jsfiddle.net/tvinci/J9ghN/
I've created a pie chart broken down by year and I've set the min and max range for the line chart based off the dimension. However, when I select a year, the range does not update. Am I doing something wrong?
var yearRingChart = dc.pieChart("#chart-ring-year");
var tpslineChart = dc.lineChart("#chart-tps");
var data2 = [
{date: "10/01/2012", hits: 1, tps: 100, http_status:200},
{date: "10/01/2012", hits: 1, tps: 90, http_status:302},
{date: "01/02/2013", hits: 1, tps: 100, http_status:200},
{date: "01/02/2013", hits: 1, tps: 10, http_status:302},
{date: "01/03/2013", hits: 0, tps: 300, http_status:200},
{date: "01/03/2013", hits: 1, tps: 10, http_status:302},
{date: "01/04/2013", hits: 1, tps: 90, http_status:200},
{date: "01/04/2013", hits: 1, tps: 0, http_status:302},
];
var ndx2 = crossfilter(data2);
// normalize/parse data
var parseDate = d3.time.format("%m/%d/%Y").parse;
data2.forEach(function(d) {
d.date = parseDate(d.date);
d.Year=d.date.getFullYear();
});
var yearDim = ndx2.dimension(function(d) {return +d.Year;});
var tpsDim = ndx2.dimension(function(d) {return d.date;});
var tps_total = yearDim.group().reduceSum(function(d) {return d.tps;});
var tps_graph = tpsDim.group().reduceSum(function(d) {return d.tps;});
var minDate = tpsDim.bottom(1)[0].date;
var maxDate = tpsDim.top(1)[0].date;
yearRingChart
.width(200).height(200)
.dimension(yearDim)
.group(tps_total)
.innerRadius(50);
tpslineChart
.width(500).height(200)
.dimension(tpsDim)
.group(tps_graph)
.brushOn(false)
.x(d3.time.scale().domain([minDate,maxDate]))
.xUnits(d3.time.day)
.yAxisLabel("TPS")
.elasticY(true)
.margins({ top: 10, left: 50, right: 10, bottom: 50 })
//.renderlet(function (chart) {chart.selectAll("g.x text").attr('dx', '-30').attr('transform', "rotate(-90)");})
;
dc.renderAll();