1
0
Fork 0

Add axises to line chart

This commit is contained in:
Gregory Eremin 2016-01-10 17:29:09 +03:00
parent 94dd074978
commit 51be9ed2d1
1 changed files with 60 additions and 27 deletions

View File

@ -179,11 +179,17 @@ var BoxPlot = React.createClass({
var LineChart = React.createClass({ var LineChart = React.createClass({
render: function() { render: function() {
var points = this.props.points, var width = 950,
maxHeight = 130, height = 120,
padding = 3; paddingLeft = 30,
paddingTop = 10,
paddingBottom = 20;
var min = 0, max; var points = this.props.points,
maxHeight = height - paddingTop - paddingBottom,
padding = 30;
var min = 0, max = 0;
points.map(function(point) { points.map(function(point) {
if (max === undefined || point.processed > max) { if (max === undefined || point.processed > max) {
max = point.processed; max = point.processed;
@ -195,49 +201,76 @@ var LineChart = React.createClass({
return; return;
} }
var path = points.map(function(point, i) { var npoints = points.map(function(point, i) {
var val = point[key]; var val = point[key];
var width = 15; var width = 15;
var x = i * width + padding; var x = i * width + padding;
var y = maxHeight - Math.round((val-min)/(max-min) * maxHeight) + 16; var y = maxHeight - Math.round((val-min)/(max-min) * maxHeight) + paddingTop;
return [x, y];
});
var path = npoints.map(function(point, i) {
var x = point[0], y = point[1];
if (i === 0) { if (i === 0) {
return "M"+x+","+y; return "M"+x+","+y;
} else { } else {
return "L"+x+","+y; return "L"+x+","+y;
} }
}).join(" ");
var dots = npoints.map(function(point, i) {
var x = point[0], y = point[1];
var r = 2; // Radius
return <circle key={"dot-"+ i}
cx={x}
cy={y}
r={r}
className={"dot "+ key} />
}); });
return ( return (
<path <g key={key}>
d={path.join(" ")} <path
className={"line "+key} /> d={path}
className={"line "+ key} />
{dots}
</g>
); );
}; };
// TODO: Define magic numbers from below here
var yMaxX = paddingLeft - 3,
yMaxY = paddingTop + 5,
xLabel1x = paddingLeft,
xLabel2x = paddingLeft + 15 * 6;
var xlabels = Array.apply(null, Array(10)).map(function(_, i){
return <text key={"x-label-"+ i}
x={paddingLeft + (15 * 6 * i)}
y={110}
textAnchor="middle"
className="axis-label">
{"-"+ (10-i) + "m"}
</text>
});
return ( return (
<div className="linechart"> <div className="linechart">
<svg> <svg>
{makePath(points, "processed")} {makePath(points, "processed")}
{makePath(points, "errors")} {makePath(points, "errors")}
<line key="y-axis" <line key="y-axis" x1={30} x2={30} y1={0} y2={100} className="axis" />
x1={3} <path key="y-arrow" d="M30,0 32,7 28,7 Z" className="arrow" />
x2={3} <text key="y-max" x={yMaxX} y={yMaxY} textAnchor="end"className="axis-label">{max}</text>
y1={0} <text key="y-zero" x={27} y={100} textAnchor="end" className="axis-label">0</text>
y2={147} <line key="x-axis" x1={30} x2={950} y1={100} y2={100} className="axis" />
className="axis" /> <path key="x-arrow" d="M950,100 943,102 943,98 Z" className="arrow" />
<path key="y-arrow" <text key="x-label-now" x={940} y={110} textAnchor="end" className="axis-label">now</text>
d="M3,0 5,7 1,7 Z" {xlabels}
className="arrow" />
<line key="x-axis"
x1={3}
x2={300}
y1={147}
y2={147}
className="axis" />
<path key="x-arrow"
d="M300,147 293,149 293,145 Z"
className="arrow" />
</svg> </svg>
</div> </div>
); );