Skip to content

Commit

Permalink
No hardcoded chart type methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Jun 15, 2015
1 parent 4047440 commit d73dae0
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 400 deletions.
1 change: 0 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ gulp.task('build', function() {
'./src/scales/**',
'./src/elements/**',
'./src/charts/**',
'./src/**',
'./node_modules/color/dist/color.min.js'
],
isCustom = !!(util.env.types),
Expand Down
2 changes: 1 addition & 1 deletion samples/bar.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
};
window.onload = function() {
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx).Bar({
window.myBar = new Chart(ctx, {
data: barChartData,
options: {
responsive: true,
Expand Down
1 change: 0 additions & 1 deletion src/charts/chart.bar.js → src/_charts/chart.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
type: "linear",
}],
},

};


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
186 changes: 0 additions & 186 deletions src/controllers/controller.canvas.rectangular.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,191 +5,5 @@
Chart = root.Chart,
helpers = Chart.helpers;

Chart.RectangularCanvasController = function(chart, elementController) {
this.chartInstance = chart;
this.elementController = elementController;
};

Chart.RectangularCanvasController.prototype.initialize = function() {
this.bindEvents();
this.buildScales();

// Need to fit scales before we reset elements.
Chart.scaleService.fitScalesForChart(this.chartInstance, this.chartInstance.chart.width, this.chartInstance.chart.height);
this.elementController.resetElements();

this.initToolTip();

this.chartInstance.update();
};

Chart.RectangularCanvasController.prototype.bindEvents = function() {
helpers.bindEvents(this.chartInstance, this.chartInstance.options.events, function(evt) {
// this will be the chart instance
this.canvasController.eventHandler(evt);
});
};

Chart.RectangularCanvasController.prototype.eventHandler = function(e) {
this.lastActive = this.lastActive || [];

// Find Active Elements
if (e.type == 'mouseout') {
this.active = [];
} else {
this.active = function() {
switch (this.chartInstance.options.hover.mode) {
case 'single':
return this.elementController.getElementAtEvent(e);
case 'label':
return this.elementController.getElementsAtEvent(e);
case 'dataset':
return this.elementController.getDatasetAtEvent(e);
default:
return e;
}
}.call(this);
}

// On Hover hook
if (this.chartInstance.options.hover.onHover) {
this.chartInstance.options.hover.onHover.call(this.chartInstance, this.active);
}

if (e.type == 'mouseup' || e.type == 'click') {
if (this.chartInstance.options.onClick) {
this.chartInstance.options.onClick.call(this, e, this.active);
}
}

var dataset;
var index;
// Remove styling for last active (even if it may still be active)
if (this.lastActive.length) {
switch (this.chartInstance.options.hover.mode) {
case 'single':
this.elementController.resetElementAppearance(this.lastActive[0], this.lastActive[0]._datasetIndex, this.lastActive[0]._index);
break;
case 'label':
for (var i = 0; i < this.lastActive.length; i++) {
this.elementController.resetElementAppearance(this.lastActive[i], this.lastActive[i]._datasetIndex, this.lastActive[i]._index);
}
break;
case 'dataset':
break;
default:
// Don't change anything
}
}

// Built in hover styling
if (this.active.length && this.chartInstance.options.hover.mode) {
switch (this.chartInstance.options.hover.mode) {
case 'single':
this.elementController.setElementHoverStyle(this.active[0]);
break;
case 'label':
for (var i = 0; i < this.active.length; i++) {
this.elementController.setElementHoverStyle(this.active[i]);
}
break;
case 'dataset':
break;
default:
// Don't change anything
}
}


// Built in Tooltips
if (this.chartInstance.options.tooltips.enabled) {

// The usual updates
this.chartInstance.tooltip.initialize();

// Active
if (this.active.length) {
this.chartInstance.tooltip._model.opacity = 1;

helpers.extend(this.chartInstance.tooltip, {
_active: this.active,
});

this.chartInstance.tooltip.update();
} else {
// Inactive
this.chartInstance.tooltip._model.opacity = 0;
}
}

// Hover animations
this.chartInstance.tooltip.pivot();

if (!this.chartInstance.animating) {
var changed;

helpers.each(this.active, function(element, index) {
if (element !== this.lastActive[index]) {
changed = true;
}
}, this);

// If entering, leaving, or changing elements, animate the change via pivot
if ((!this.lastActive.length && this.active.length) ||
(this.lastActive.length && !this.active.length) ||
(this.lastActive.length && this.active.length && changed)) {

this.chartInstance.stop();
this.chartInstance.render(this.chartInstance.options.hover.animationDuration);
}
}

// Remember Last Active
this.lastActive = this.active;
return this;
};

Chart.RectangularCanvasController.prototype.initToolTip = function() {
this.chartInstance.tooltip = new Chart.Tooltip({
_chart: this.chartInstance.chart,
_data: this.chartInstance.data,
_options: this.chartInstance.options,
}, this);
};

Chart.RectangularCanvasController.prototype.buildScales = function() {
// Map of scale ID to scale object so we can lookup later
this.chartInstance.scales = {};

// Build the x axes
helpers.each(this.chartInstance.options.scales.xAxes, function(xAxisOptions) {
var ScaleClass = Chart.scaleService.getScaleConstructor(xAxisOptions.type);
var scale = new ScaleClass({
ctx: this.chartInstance.chart.ctx,
options: xAxisOptions,
data: this.chartInstance.data,
id: xAxisOptions.id,
});

this.chartInstance.scales[scale.id] = scale;
}, this);

// Build the y axes
helpers.each(this.chartInstance.options.scales.yAxes, function(yAxisOptions) {
var ScaleClass = Chart.scaleService.getScaleConstructor(yAxisOptions.type);
var scale = new ScaleClass({
ctx: this.chartInstance.chart.ctx,
options: yAxisOptions,
data: this.chartInstance.data,
id: yAxisOptions.id,
});

this.chartInstance.scales[scale.id] = scale;
}, this);
};

Chart.RectangularCanvasController.prototype.update = function() {
Chart.scaleService.fitScalesForChart(this.chartInstance, this.chartInstance.chart.width, this.chartInstance.chart.height);
this.elementController.updateElements();
};
}).call(this);
Loading

0 comments on commit d73dae0

Please sign in to comment.