Auto Scale Axis (#3070)

* fix input to chart that was causes scales to not auto size

* formatting

* formatting

* added comments
This commit is contained in:
Anthony Dresser
2018-10-31 20:27:40 -07:00
committed by Karl Burtram
parent db2d380b6c
commit 2080c525a7
2 changed files with 30 additions and 17 deletions

View File

@@ -326,7 +326,7 @@ export class ChartView extends Disposable implements IPanelView {
this.optionDisposables.push(attachInputBoxStyler(numberInput, this._themeService)); this.optionDisposables.push(attachInputBoxStyler(numberInput, this._themeService));
break; break;
case ControlType.dateInput: case ControlType.dateInput:
let dateInput = new InputBox(optionContainer, this._contextViewService, { type: 'date' }); let dateInput = new InputBox(optionContainer, this._contextViewService, { type: 'datetime-local' });
dateInput.value = value || ''; dateInput.value = value || '';
dateInput.onDidChange(e => { dateInput.onDidChange(e => {
if (this.options[option.configEntry] !== e) { if (this.options[option.configEntry] !== e) {

View File

@@ -19,7 +19,7 @@ import { ChartType, DataDirection, LegendPosition, DataType, IPointDataSet, cust
const noneLineGraphs = [ChartType.Doughnut, ChartType.Pie]; const noneLineGraphs = [ChartType.Doughnut, ChartType.Pie];
const timeSeriesScales = { const timeSeriesScales: ChartJs.ChartOptions = {
scales: { scales: {
xAxes: [{ xAxes: [{
type: 'time', type: 'time',
@@ -64,7 +64,7 @@ export class Graph implements IInsight {
this._theme = e; this._theme = e;
this.data = this._data; this.data = this._data;
}); });
this._options = mixin(options, defaultOptions, false); this.options = mixin(options, defaultOptions, false);
let canvasContainer = document.createElement('div'); let canvasContainer = document.createElement('div');
canvasContainer.style.width = '100%'; canvasContainer.style.width = '100%';
@@ -89,9 +89,12 @@ export class Graph implements IInsight {
} }
public set data(data: IInsightData) { public set data(data: IInsightData) {
if (!data) {
return;
}
this._data = data; this._data = data;
let chartData: Array<ChartJs.ChartDataSets>;
let labels: Array<string>; let labels: Array<string>;
let chartData: Array<ChartJs.ChartDataSets>;
if (this.options.dataDirection === DataDirection.Horizontal) { if (this.options.dataDirection === DataDirection.Horizontal) {
if (this.options.labelFirstColumn) { if (this.options.labelFirstColumn) {
@@ -158,19 +161,19 @@ export class Graph implements IInsight {
if (this.chartjs) { if (this.chartjs) {
this.chartjs.data.datasets = chartData; this.chartjs.data.datasets = chartData;
this.chartjs.config.type = this.options.type; this.chartjs.config.type = this.options.type;
this.chartjs.data.labels = labels; // we don't want to include lables for timeSeries
this.chartjs.data.labels = this.originalType === 'timeSeries' ? [] : labels;
this.chartjs.options = this.transformOptions(this.options); this.chartjs.options = this.transformOptions(this.options);
this.chartjs.update(0); this.chartjs.update(0);
} else { } else {
this.chartjs = new ChartJs(this.canvas.getContext('2d'), { this.chartjs = new ChartJs(this.canvas.getContext('2d'), {
data: { data: {
labels: labels, // we don't want to include lables for timeSeries
labels: this.originalType === 'timeSeries' ? [] : labels,
datasets: chartData datasets: chartData
}, },
type: this.options.type, type: this.options.type,
options: { options: this.transformOptions(this.options)
maintainAspectRatio: false
}
}); });
} }
} }
@@ -197,15 +200,21 @@ export class Graph implements IInsight {
display: options.xAxisLabel ? true : false display: options.xAxisLabel ? true : false
}, },
ticks: { ticks: {
fontColor: foreground, fontColor: foreground
max: options.xAxisMax,
min: options.xAxisMin
}, },
gridLines: { gridLines: {
color: gridLines color: gridLines
} }
}]; }];
if (options.xAxisMax) {
retval.scales = mixin(retval.scales, { xAxes: [{ ticks: { max: options.xAxisMax } }] }, true, customMixin);
}
if (options.xAxisMin) {
retval.scales = mixin(retval.scales, { xAxes: [{ ticks: { min: options.xAxisMin } }] }, true, customMixin);
}
retval.scales.yAxes = [{ retval.scales.yAxes = [{
scaleLabel: { scaleLabel: {
fontColor: foreground, fontColor: foreground,
@@ -213,22 +222,27 @@ export class Graph implements IInsight {
display: options.yAxisLabel ? true : false display: options.yAxisLabel ? true : false
}, },
ticks: { ticks: {
fontColor: foreground, fontColor: foreground
max: options.yAxisMax,
min: options.yAxisMin
}, },
gridLines: { gridLines: {
color: gridLines color: gridLines
} }
}]; }];
if (options.yAxisMax) {
retval.scales = mixin(retval.scales, { yAxes: [{ ticks: { max: options.yAxisMax } }] }, true, customMixin);
}
if (options.yAxisMin) {
retval.scales = mixin(retval.scales, { yAxes: [{ ticks: { min: options.yAxisMin } }] }, true, customMixin);
}
if (this.originalType === ChartType.TimeSeries) { if (this.originalType === ChartType.TimeSeries) {
retval = mixin(retval, timeSeriesScales, true, customMixin); retval = mixin(retval, timeSeriesScales, true, customMixin);
if (options.xAxisMax) { if (options.xAxisMax) {
retval = mixin(retval, { retval = mixin(retval, {
scales: { scales: {
xAxes: [{ xAxes: [{
type: 'time',
time: { time: {
max: options.xAxisMax max: options.xAxisMax
} }
@@ -241,7 +255,6 @@ export class Graph implements IInsight {
retval = mixin(retval, { retval = mixin(retval, {
scales: { scales: {
xAxes: [{ xAxes: [{
type: 'time',
time: { time: {
min: options.xAxisMin min: options.xAxisMin
} }