Commit fccc553d authored by Evgeny's avatar Evgeny

Fix problem with stacked-100

parent 0f0da68a
......@@ -36,6 +36,7 @@ module.exports = (grunt) ->
'src/shape.js',
'src/shape.line.js',
'src/shape.bar.js',
'src/stacked.js',
'src/text.js',
'src/type.js',
'src/grid.js',
......
......@@ -685,6 +685,8 @@
$$.y2Axis.tickValues($$.generateTickValues($$.y2.domain(), config.axis_y2_tick_count));
}
$$.normalize();
// axes
$$.redrawAxis(transitions, hideAxis);
......@@ -1302,7 +1304,7 @@
pie_label_format: undefined,
pie_label_threshold: 0.05,
pie_expand: true,
explodeRadius = 0,
explodeRadius: 0,
// gauge
gauge_label_show: true,
gauge_label_format: undefined,
......@@ -1332,7 +1334,9 @@
},
tooltip_init_show: false,
tooltip_init_x: 0,
tooltip_init_position: {top: '0px', left: '50px'}
tooltip_init_position: {top: '0px', left: '50px'},
// caching
shouldCache: true
};
Object.keys(this.additionalConfig).forEach(function (key) {
......@@ -3378,6 +3382,29 @@
return sx < mouse[0] && mouse[0] < ex && ey < mouse[1] && mouse[1] < sy;
};
c3.chart.internal.fn.normalize = function(){
var $$ = this, tr = [], c = [],
data = $$.api.data();
if(!$$.config.normalized){
return;
}
for(var k = 0; k < data[0].values.length; k++) {
var tt = 0;
for(c in data) {
tt = tt + data[c].values[k].value;
}
tr[k] = tt;
}
for(c in data) {
for(k = 0; k < tr.length; k++) {
data[c].values[k].value = data[c].values[k].value / tr[k];
}
}
}
c3_chart_internal_fn.initText = function () {
var $$ = this;
$$.main.select('.' + CLASS.chart).append("g")
......@@ -4635,39 +4662,42 @@
$$.pushCallback(callback);
if(!$$.config.stacked){
// if we don't need to tune
if($$.config.stacked){
$$.cachedRedraw();
return;
}
var apply = function(){
var apply = function(){
var minmax = $$.findMinMax();
var minmax = $$.findMinMax();
var sizesX;
if($$.config.is_xy){
sizesX = $$.getAbscissa($$.config.ed3Type, minmax.minX, minmax.maxX);
var sizesX;
if($$.config.is_xy){
sizesX = $$.getAbscissa($$.config.ed3Type, minmax.minX, minmax.maxX);
}
var sizesY = $$.getAxisData(minmax.minY, minmax.maxY);
$$.api.axis.range({
min: {
x: sizesX ? sizesX.min : undefined,
y: sizesY.min
},
max: {
x: sizesX ? sizesX.max : undefined,
y: sizesY.max
}
var sizesY = $$.getAxisData(minmax.minY, minmax.maxY);
$$.api.axis.range({
min: {
x: sizesX ? sizesX.min : undefined,
y: sizesY.min
},
max: {
x: sizesX ? sizesX.max : undefined,
y: sizesY.max
}
});
});
$$.resolveCallbacks();
$$.resolveCallbacks();
};
};
if(sync){
apply();
} else {
$$.buffer.onlastfinish("tune-axis", apply);
$$.buffer.onlastfinish("cached-redraw", function(){});
}
if(sync){
apply();
} else {
$$.buffer.onlastfinish("tune-axis", apply);
$$.buffer.onlastfinish("cached-redraw", function(){});
}
};
......@@ -4951,8 +4981,7 @@
c3_chart_internal_fn.getClipPath = function (id) {
var isIE9 = window.navigator.appVersion.toLowerCase().indexOf("msie 9.") >= 0;
return "url(" + (isIE9 ? "" : document.URL.split('#')[0]) + "#" + id + ")";
return "url(" + "#" + id + ")";
};
c3_chart_internal_fn.appendClip = function (parent, id) {
return parent.append("clipPath").attr("id", id).append("rect");
......@@ -5324,12 +5353,12 @@
};
c3.chart.internal.fn.getAngle = function(d){
c3_chart_internal_fn.getAngle = function(d){
var $$ = this;
$$.config.newd = d;
};
c3.chart.internal.fn.wrapExplode = function(){
c3_chart_internal_fn.wrapExplode = function(){
var $$ = this;
explode.offset = $$.config.explodeRadius;
......@@ -5340,10 +5369,9 @@
return explode;
};
// for exploding pie
function explode(d, index) {
d = explode.updateAngle(d);
if(utils.isNull(d)){
if(isNull(d)){
return "";
}
var offset = explode.offset;
......@@ -5357,7 +5385,7 @@
explode.getAngle(d);
var turnAngle = ((d.startAngle - d.endAngle) / 2 % (2 * Math.PI)) + utils.toRadians(90);
var turnAngle = ((d.startAngle - d.endAngle) / 2 % (2 * Math.PI)) + toRadians(90);
explode.turn({
radians: turnAngle
});
......@@ -6052,7 +6080,7 @@
$$.pushCallback(callback);
if($$.shouldCache){
if($$.config.shouldCache){
if(!$$.buffer.has("tune-axis")){
$$.buffer.onlastfinish("cached-redraw",
function(){
......@@ -6262,6 +6290,9 @@
isUndefined = c3_chart_internal_fn.isUndefined = function (v) {
return typeof v === 'undefined';
},
isNull = c3_chart_internal_fn.isNull = function(v){
return v === null;
},
isDefined = c3_chart_internal_fn.isDefined = function (v) {
return typeof v !== 'undefined';
},
......@@ -6305,60 +6336,12 @@
c3_chart_fn.focus = function (targetIds) {
var $$ = this.internal, candidates;
targetIds = $$.mapToTargetIds(targetIds);
candidates = $$.svg.selectAll($$.selectorTargets(targetIds.filter($$.isTargetToShow, $$))),
this.revert();
this.defocus();
candidates.classed(CLASS.focused, true).classed(CLASS.defocused, false);
if ($$.hasArcType()) {
$$.expandArc(targetIds);
}
$$.toggleFocusLegend(targetIds, true);
$$.focusedTargetIds = targetIds;
$$.defocusedTargetIds = $$.defocusedTargetIds.filter(function (id) {
return targetIds.indexOf(id) < 0;
});
};
c3_chart_fn.defocus = function (targetIds) {
var $$ = this.internal, candidates;
targetIds = $$.mapToTargetIds(targetIds);
candidates = $$.svg.selectAll($$.selectorTargets(targetIds.filter($$.isTargetToShow, $$))),
this.revert();
candidates.classed(CLASS.focused, false).classed(CLASS.defocused, true);
if ($$.hasArcType()) {
$$.unexpandArc(targetIds);
}
$$.toggleFocusLegend(targetIds, false);
$$.focusedTargetIds = $$.focusedTargetIds.filter(function (id) {
return targetIds.indexOf(id) < 0;
});
$$.defocusedTargetIds = targetIds;
};
c3_chart_fn.revert = function (targetIds) {
var $$ = this.internal, candidates;
targetIds = $$.mapToTargetIds(targetIds);
candidates = $$.svg.selectAll($$.selectorTargets(targetIds)); // should be for all targets
candidates.classed(CLASS.focused, false).classed(CLASS.defocused, false);
if ($$.hasArcType()) {
$$.unexpandArc(targetIds);
}
if ($$.config.legend_show) {
$$.showLegend(targetIds.filter($$.isLegendToShow.bind($$)));
}
$$.focusedTargetIds = [];
$$.defocusedTargetIds = [];
};
c3_chart_fn.title = function(title, hasSubs, isSub) {
......@@ -6512,6 +6495,9 @@
c3_chart_fn.load = function (args) {
var $$ = this.internal, config = $$.config;
$$.config.turned = false;
// update xs if specified
if (args.xs) {
$$.addXs(args.xs);
......@@ -6599,35 +6585,29 @@
c3_chart_fn.setValue = function(id, i, value){
var $$ = this.internal;
var column = $$.api.getDataById(id);
if(isUndefined(column)){
throw new Error("Setting value to non-existing sequence " + id);
}
var t = $$.api.data(id)[0];
if(isUndefined(column[i])){
if(!t.values[i]){
$$.api.appendToColumn([id, value]);
} else {
column[i] = value;
$$.api.loadColumns([[id].concat(column)]);
t.values[i].value = value;
$$.tuneAxis();
}
};
c3_chart_fn.getValue = function(id, i){
var $$ = this.internal;
var column = $$.api.getDataById(id);
if(isUndefined(column)){
var t = $$.api.data(id)[0];
if(!t){
return undefined;
}
var v = column[i];
if(isUndefined(v)){
if(!t.values[i]){
return undefined;
}
return t.values[i].value;
return v.value;
};
c3_chart_fn.flow = function (args) {
......@@ -7005,6 +6985,14 @@
return config.data_groups;
};
c3_chart_fn.isNormalized = function(is){
var $$ = this.internal;
if(isUndefined(is)){
return $$.config.normalized;
}
$$.config.normalized = is;
};
c3_chart_fn.xgrids = function (grids) {
var $$ = this.internal, config = $$.config;
if (! grids) { return config.grid_x_lines; }
......@@ -7769,4 +7757,4 @@
window.c3 = c3;
}
})(window);
})(this);
......@@ -5,3 +5,11 @@ c3_chart_fn.groups = function (groups) {
$$.redraw();
return config.data_groups;
};
c3_chart_fn.isNormalized = function(is){
var $$ = this.internal;
if(isUndefined(is)){
return $$.config.normalized;
}
$$.config.normalized = is;
};
......@@ -496,6 +496,8 @@ c3_chart_internal_fn.redraw = function (options, transitions) {
$$.y2Axis.tickValues($$.generateTickValues($$.y2.domain(), config.axis_y2_tick_count));
}
$$.normalize();
// axes
$$.redrawAxis(transitions, hideAxis);
......
c3.chart.internal.fn.drawLine = function(line, x1, x2, y1, y2){
line.attr("class", "lineForSubChart")
.attr("x1", x1)
.attr("x2", x2)
.attr("y1", y1)
.attr("y2", y2)
.style("stroke-width", 1)
.style("stroke", "#aaa");
};
c3.chart.internal.fn.getBox = function(selection){
return selection.node().getBBox();
};
c3.chart.internal.fn.getLineCoordsForBar = function(center, order){
var $$ = this;
var x1, x2, y1, y2;
if(utils.isSub($$.config.ed3Type)){
$$.ed3Config.subBox = $$.getBox($$.main.selectAll(".sub-chart .c3-chart-bars"));
if(!$$.ed3Config.coords[order]) return;
var coords = $$.ed3Config.coords[order];
x1 = coords.x1;
x2 = coords.x2;
y1 = coords.y1;
y2 = coords.y2;
x1 -= ($$.getCurrentWidth());
x2 -= ($$.getCurrentWidth());
} else {
$$.config.angle = $$.updateAngle($$.config.newd).startAngle;
// Does first sequence take less than half of the chart?
var small = toDegrees($$.config.angle) > 0;
x1 = center.x;
if(small){
x1 += $$.radius * Math.sin($$.config.angle);
}
if(order === 0){
y1 = center.y - $$.radius * (small ? Math.cos($$.config.angle) : 1);
} else {
y1 = center.y + $$.radius * (small ? Math.cos($$.config.angle) : 1);
}
if(!$$.ed3Config.coords){
$$.ed3Config.coords = {};
}
if(!$$.ed3Config.subBox) return;
x2 = $$.ed3Config.subBox.x + $$.getCurrentWidth();
y2 = $$.ed3Config.subBox.y;
if(order === 1){
y2 += $$.ed3Config.subBox.height;
}
$$.ed3Config.coords[order] = {
x1: x1,
x2: x2,
y1: y1,
y2: y2
};
}
return {
x1: x1,
x2: x2,
y1: y1,
y2: y2
};
};
c3.chart.internal.fn.getLineCoordsForPie = function(center, order){
var $$ = this;
var x1, x2, y1, y2;
if(utils.isSub($$.config.ed3Type)){
if(!$$.ed3Config.coords) return;
var coords = $$.ed3Config.coords[order];
x1 = coords.x1;
x2 = coords.x2;
y1 = coords.y1;
y2 = coords.y2;
x1 -= ($$.getCurrentWidth());
x2 -= ($$.getCurrentWidth());
} else {
$$.config.angle = $$.updateAngle($$.config.newd).startAngle;
// Does first sequence take less than half of the chart?
var small = toDegrees($$.config.angle) > 0;
x1 = center.x;
if(small){
x1 += $$.radius * Math.sin($$.config.angle);
}
x2 = x1 + $$.getCurrentWidth();
if(order === 0){
y1 = center.y - $$.radius * (small ? Math.cos($$.config.angle) : 1);
y2 = center.y - $$.radius;
} else {
y1 = center.y + $$.radius * (small ? Math.cos($$.config.angle) : 1);
y2 = center.y + $$.radius;
}
if(!$$.ed3Config.coords){
$$.ed3Config.coords = {};
}
if(small){
var subCenter = {
x: center.x + $$.getCurrentWidth(),
y: center.y
};
// middle circle
var x3 = (center.x + subCenter.x) / 2;
var y3 = (center.y + subCenter.y) / 2;
var r3 = Math.sqrt((x3 - center.x) * (x3 - center.x) + (y3 - center.y) * (y3 - center.y));
var r4 = $$.radius;
// get intersection of middle and sub circles
var e = (subCenter.x - x3);
var f = (subCenter.y - y3);
// http://mathforum.org/library/drmath/view/51836.html
var p = Math.sqrt(e*e + f*f);
var k = (p*p + r3*r3 - r4*r4) / ( 2 * p);
if(order === 1){
x2 = x3 + e*k/p - (f/p) * Math.sqrt(r3*r3 - k*k);
y2 = y3 + f*k/p + (e/p) * Math.sqrt(r3*r3 - k*k);
} else {
x2 = x3 + e*k/p + (f/p) * Math.sqrt(r3*r3 - k*k);
y2 = y3 + f*k/p - (e/p) * Math.sqrt(r3*r3 - k*k);
}
}
$$.ed3Config.coords[order] = {
x1: x1,
x2: x2,
y1: y1,
y2: y2
};
}
return {
x1: x1,
x2: x2,
y1: y1,
y2: y2
};
};
c3.chart.internal.fn.redrawLines = function(){
var line;
var $$ = this;
var main = $$.main;
// If line wasn't created yet
if(main.selectAll(".lineForSubChart")[0].length === 0){
line = main.append("line");
} else {
line = main.selectAll(".lineForSubChart").filter(function(d, i){ return i === 0; });
}
center = $$.getCenter(main.selectAll('.c3-chart'));
var coords;
if($$.config.subType === "sub-pie"){
coords = $$.getLineCoordsForPie(center, 0);
} else if($$.config.subType === "sub-bar"){
coords = $$.getLineCoordsForBar(center, 0);
}
if(!coords) return;
$$.drawLine(line, coords.x1, coords.x2, coords.y1, coords.y2);
// If only previous line was created
if(main.selectAll(".lineForSubChart")[0].length === 1){
line = main.append("line");
} else {
line = main.selectAll(".lineForSubChart").filter(function(d, i){ return i === 1; });
}
center = $$.getCenter(main.selectAll('.c3-chart'));
if($$.config.subType === "sub-pie"){
coords = $$.getLineCoordsForPie(center, 1);
} else if($$.config.subType === "sub-bar"){
coords = $$.getLineCoordsForBar(center, 1);
}
if(!coords) return;
$$.drawLine(line, coords.x1, coords.x2, coords.y1, coords.y2);
};
c3.chart.internal.fn.getCenter = function(selection){
var $$ = this;
var bbox = $$.getBox(selection);
return {
x: bbox.x + bbox.width/2,
y: bbox.y + bbox.height/2
};
};
c3.chart.internal.fn.normalize = function(){
var $$ = this, tr = [], c = [],
data = $$.api.data();
if(!$$.config.normalized){
return;
}
for(var k = 0; k < data[0].values.length; k++) {
var tt = 0;
for(c in data) {
tt = tt + data[c].values[k].value;
}
tr[k] = tt;
}
for(c in data) {
for(k = 0; k < tr.length; k++) {
data[c].values[k].value = data[c].values[k].value / tr[k];
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment