Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fill on stacked lines (II) #186

Closed
wants to merge 12 commits into from
2 changes: 1 addition & 1 deletion flotr2.ie.min.js

Large diffs are not rendered by default.

77 changes: 59 additions & 18 deletions flotr2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3560,19 +3560,11 @@ Flotr.addType('lines', {
if (start === null) start = data[i];

if (stack) {

stack1 = stack.values[data[i][0]] || 0;
stack2 = stack.values[data[i+1][0]] || stack.values[data[i][0]] || 0;

y1 = yScale(data[i][1] + stack1);
y2 = yScale(data[i+1][1] + stack2);

if(incStack){
stack.values[data[i][0]] = data[i][1]+stack1;

if(i == length-1)
stack.values[data[i+1][0]] = data[i+1][1]+stack2;
}
y2 = yScale(data[i+1][1] + stack2);
}
else{
y1 = yScale(data[i][1]);
Expand Down Expand Up @@ -3603,22 +3595,71 @@ Flotr.addType('lines', {

fill();

if (stack) {
for (i = 0; i < length; ++i) {
stack1 = stack.values[data[i][0]] || 0;
stack2 = stack.values[data[i+1][0]] ||
stack.values[data[i][0]] || 0;
if (incStack) {
stack.values[data[i][0]] = data[i][1]+stack1;
if (i == length-1)
stack.values[data[i+1][0]] = data[i+1][1]+stack2;
}
}
}

function drawPathRev(data) {
for (i = length-1; i >= 0 ; --i) {
if (!options.fill) return;

// Empty values not full supported
if (!data[i]) return;
if (data[i][1] === null) {
data[i][1] = 0;
}

x = xScale(data[i][0]);
y = yScale(data[i][1]);

if (
(y > height) || (x > width) ||
(y < 0) || (x < 0)
) return;
context.lineTo(x, y);
}
}

function stackToPlot(data) {
var stack_data = [];
for (i = 0; i < data.length; ++i) {
stack_data.push([i, data[i]]);
}
return stack_data;
}

function fill () {
// TODO stacked lines
if(!shadowOffset && options.fill && start){
x1 = xScale(start[0]);
context.fillStyle = options.fillStyle;
context.lineTo(x2, zero);
context.lineTo(x1, zero);
context.lineTo(x1, yScale(start[1]));
context.fill();
if (options.fillBorder) {
context.stroke();
}
x1 = xScale(start[0]);
context.fillStyle = options.fillStyle;
if (!stack || stack.values.length === 0) {
context.lineTo(x2, zero);
context.lineTo(x1, zero);
context.lineTo(x1, yScale(start[1]));
}
else {
var stack_plot = stackToPlot(stack.values);
stack_x1 = xScale(stack_plot[0][0]);
stack_y2 = yScale(stack_plot[stack_plot.length-1][1]);
context.lineTo(x2, stack_y2);
drawPathRev(stack_plot);
context.lineTo(stack_x1, yScale(data[0][1]));
}
context.fill();
}
}

context.closePath();
},

// Perform any pre-render precalculations (this should be run on data first)
Expand Down
Loading