Skip to content

Commit

Permalink
Add example for dynamic resize.
Browse files Browse the repository at this point in the history
  • Loading branch information
fanthos committed Apr 24, 2018
1 parent e8a37fd commit 0cbc5cd
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,63 @@ Timeline chart library for Chart.js.
},
```

Example for dynamic resize by dataset count:
```javascript
resizeChart() {
if (!this._chart) return;
// Chart not ready
if (this.$.chartTarget.clientWidth === 0) {
if (this._resizeTimer === undefined) {
this._resizeTimer = setInterval(this.resizeChart.bind(this), 10);
return;
}
}

clearInterval(this._resizeTimer);
this._resizeTimer = undefined;

this._resizeChart();
}

_resizeChart() {
const chartTarget = this.$.chartTarget;

const options = this.data;
const data = options.data;

if (data.datasets.length === 0) {
return;
}

if (!this.isTimeline) {
this._chart.resize();
return;
}

// Recalculate chart height for Timeline chart
var axis = this._chart.boxes.filter(x => x.position === 'bottom')[0];
if (axis && axis.height > 0) {
this._axisHeight = axis.height;
}
if (!this._axisHeight) {
chartTarget.style.height = '100px';
chartTarget.height = '100px';
this._chart.resize();
axis = this._chart.boxes.filter(x => x.position === 'bottom')[0];
if (axis && axis.height > 0) {
this._axisHeight = axis.height;
}
}
if (this._axisHeight) {
const cnt = data.datasets.length;
const targetHeight = ((30 * cnt) + this._axisHeight) + 'px';
if (chartTarget.style.height !== targetHeight) {
chartTarget.style.height = targetHeight;
chartTarget.height = targetHeight;
}
this._chart.resize();
}
}
```

Usage: https://github.com/fanthos/chartjs-chart-timeline/wiki

0 comments on commit 0cbc5cd

Please sign in to comment.