Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Creates base chart component using Chart.js #91
Browse files Browse the repository at this point in the history
  • Loading branch information
zenorocha committed Mar 23, 2018
1 parent b965dcd commit 4c45a93
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
32 changes: 31 additions & 1 deletion packages/marble-chart/demos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,37 @@ <h1 class="page-title">Chart</h1>
<div id="container"></div>

<script>
new metal.Chart({}, '#container');
window.chart = new metal.Chart({
type: 'pie',
data: {
labels: [
'Foo',
'Bar'
],
datasets: [
{
data: [
10,
45
],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)'
],
borderWidth: 1
}
]
},
options: {
legend: {
display: false
}
}
}, '#container');
</script>
</body>
</html>
1 change: 1 addition & 0 deletions packages/marble-chart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"metal"
],
"dependencies": {
"chart.js": "^2.7.2",
"metal-component": "^2.14.0",
"metal-soy": "^2.14.0",
"metal-state": "^2.14.0"
Expand Down
50 changes: 46 additions & 4 deletions packages/marble-chart/src/Chart.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
import {isServerSide} from 'metal';
import Component from 'metal-component';
import Soy from 'metal-soy';
import {Config} from 'metal-state';
import ChartJS from 'chart.js';

import templates from './Chart.soy.js';

/**
* Chart component.
*/
class Chart extends Component {}
class Chart extends Component {
attached() {
if (isServerSide()) {
return;
}

this.chart = new ChartJS(this.refs.canvas, {
type: this.type,
data: this.data,
options: this.options,
});
}

willReceiveState(changes) {
if (changes.data) {
this.chart.data = changes.data.newVal;
this.chart.update();
}
}

disposed() {
this.chart.destroy();
}
}

/**
* State definition.
Expand All @@ -16,11 +41,28 @@ class Chart extends Component {}
*/
Chart.STATE = {
/**
* ID to be applied to the element.
* Built-in types for Chart.js
* @type {!String}
* @default example
*/
id: Config.string().value('example'),
type: {
value: Config.oneOf(['bar', 'bubble', 'line', 'pie', 'polarArea', 'radar']),
},

/**
* Data used to render Chart.js
* @type {!Object}
*/
data: {
value: {}
},

/**
* Styling options for Chart.js
* @type {!Object}
*/
options: {
value: {}
}
};

Soy.register(Chart, templates);
Expand Down
4 changes: 1 addition & 3 deletions packages/marble-chart/src/Chart.soy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@
* This renders the component's whole content.
*/
{template .render}
{@param? id: string}

<div id="{$id}">Hello World</div>
<canvas ref="canvas"></canvas>
{/template}

0 comments on commit 4c45a93

Please sign in to comment.