Skip to content

Commit d573fce

Browse files
committed
support Renderer
1 parent a4c9413 commit d573fce

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import VueHighcharts from 'vue-highcharts';
2222
Vue.use(VueHighcharts);
2323
```
2424

25-
If you want to use Highstock, Highmaps or any add-ons, you should pass in the `Highcharts` object [which included the corresponding modules](http://www.highcharts.com/docs/getting-started/install-from-npm).
25+
If you want to use Highstock, Highmaps or any other add-ons, you should pass in the `Highcharts` object [which included the corresponding modules](http://www.highcharts.com/docs/getting-started/install-from-npm).
2626

2727
```js
2828
// Use Highstock
@@ -45,12 +45,27 @@ loadMap(Highcharts);
4545
Vue.use(VueHighcharts, { Highcharts });
4646
```
4747

48-
Then you can use the component in your template. And you can access the `Highcharts` object via `vm.Highcharts`.
48+
Then you can use the components in your template.
4949

5050
```html
5151
<highcharts :options="options"></highcharts>
5252
<highstock :options="options"></highstock>
5353
<highmaps :options="options"></highmaps>
54+
<highcharts-renderer :width="width" :height="height"></highcharts-renderer>
5455
```
5556

5657
The `options` object can be found in [Highcharts API Reference](http://api.highcharts.com/highcharts), [Highstock API Reference](http://api.highcharts.com/highstock) and [Highmaps API Reference](http://api.highcharts.com/highmaps).
58+
59+
`<highcharts-renderer>` [creates an independent renderer](http://api.highcharts.com/highcharts/Renderer).
60+
61+
The `Highcharts` object is available at `vm.Highcharts`. If you want to access the `chart` or `renderer` instance, you can use child component refs:
62+
63+
```html
64+
<highcharts :options="options" ref="highcharts"></highcharts>
65+
<highcharts-renderer :width="width" :height="height" ref="highchartsRenderer"></highcharts-renderer>
66+
```
67+
68+
```js
69+
const { chart } = vm.$refs.highcharts;
70+
const { renderer } = vm.$refs.highchartsRenderer;
71+
```

src/constrators.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
highcharts: 'Chart',
3+
highstock: 'StockChart',
4+
highmaps: 'Map',
5+
'highcharts-renderer': 'Renderer'
6+
};

src/create.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
1+
import ctors from './constrators.js';
2+
13
function create(tagName, Highcharts) {
2-
var ctors = {
3-
highcharts: 'Chart',
4-
highstock: 'StockChart',
5-
highmaps: 'Map'
6-
};
74
var Ctor = Highcharts[ctors[tagName]];
85
if (!Ctor) {
96
return null;
107
}
8+
var isRenderer = tagName === 'highcharts-renderer';
119
return {
1210
name: tagName,
1311
template: '<div></div>',
14-
props: {
15-
options: Object
16-
},
12+
props: isRenderer
13+
? {
14+
width: { type: Number, required: true },
15+
height: { type: Number, required: true }
16+
}
17+
: { options: { type: Object, required: true } },
1718
methods: {
18-
render: function(options) {
19-
var opts = options || {};
20-
opts.chart = opts.chart || {};
21-
opts.chart.renderTo = this.$el;
22-
this._chart = new Ctor(opts);
19+
_renderChart: function() {
20+
if (isRenderer) {
21+
this.renderer = new Ctor(this.$el, this.width, this.height);
22+
} else {
23+
var opts = JSON.parse(JSON.stringify(this.options));
24+
opts.chart = opts.chart || {};
25+
opts.chart.renderTo = this.$el;
26+
this.chart = new Ctor(opts);
27+
}
2328
}
2429
},
2530
mounted: function() {
26-
this.render(this.options);
31+
this._renderChart();
2732
},
2833
beforeDestroy: function() {
29-
this._chart.destroy();
34+
!isRenderer && this.chart.destroy();
3035
}
3136
};
3237
}

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import HighchartsOnly from 'highcharts';
2+
import ctors from './constrators.js';
23
import create from './create.js';
34

45
function install(Vue, options) {
56
var Highcharts = (options && options.Highcharts) || HighchartsOnly;
67
Vue.prototype.Highcharts = Highcharts;
7-
['highcharts', 'highstock', 'highmaps'].forEach(function(tagName) {
8+
for (var tagName in ctors) {
89
var component = create(tagName, Highcharts);
910
component && Vue.component(tagName, component);
10-
});
11+
}
1112
}
1213

1314
export default install;

0 commit comments

Comments
 (0)