エクスポート

チャートを他のドキュメントに含めるためのイメージとして保存することは一般的な要件です。一部のブラウザでは、これをコンテキストメニューのオプションとしてサポートしていますが、すべてではありません。

FlexChartのsaveImageToFileメソッドはこれを簡単にします。

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as wjChart from '@grapecity/wijmo.chart'; import '@grapecity/wijmo.chart.render'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // create the chart let theChart = new wjChart.FlexChart('#theChart', { header: 'フェーズ2 テスト結果', chartType: 'Scatter', axisY: { axisLine: true }, series: [ { name: '実験1', itemsSource: getData(50, 0, 3), bindingX: 'x', binding: 'y' }, { name: '実験2', itemsSource: getData(40, 100, 12), bindingX: 'x', binding: 'y' }, { name: '実験3', itemsSource: getData(30, -100, 24), bindingX: 'x', binding: 'y' } ] }); // // save chart image to file document.querySelector('#saveButtons').addEventListener('click', e => { if (e.target instanceof HTMLButtonElement) { theChart.saveImageToFile('FlexChart.' + e.target.textContent); } }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexChart Export</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <p id="saveButtons"> 名前を付けて保存: <button class="btn btn-default">PNG</button> <button class="btn btn-default">JPEG</button> <button class="btn btn-default">SVG</button> </p> <div id="theChart"></div> </div> </body> </html> // data sources export function getData(cnt, a, b) { let arr = [], x = -5 * cnt / 2; // for (let i = 0; i < cnt; i++) { let rnd = Math.random() * cnt - cnt / 2; // arr.push({ x: x, y: a + x * (b + rnd) + rnd }); // x += .5 + Math.random() * 10; } // return arr; } .wj-flexchart { height: 300px; margin: 10px 0; background: white; } body { margin-bottom: 24pt; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import { Component, Inject, enableProdMode, NgModule, ViewChild } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { WjChartModule } from '@grapecity/wijmo.angular2.chart'; import { DataService } from './app.data'; import * as wjChart from '@grapecity/wijmo.chart'; import '@grapecity/wijmo.chart.render'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data1: any[]; data2: any[]; data3: any[]; @ViewChild('theChart') theChart: wjChart.FlexChart; // constructor(@Inject(DataService) private dataService: DataService) { this.data1 = dataService.getData(50, 0, 3); this.data2 = dataService.getData(40, 100, 12); this.data3 = dataService.getData(30, -100, 24); } // export(type: string) { this.theChart.saveImageToFile('FlexChart.' + type); } } // @NgModule({ imports: [WjChartModule, BrowserModule], declarations: [AppComponent], providers: [DataService], bootstrap: [AppComponent] }) export class AppModule { } // enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexChart Export</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <div class="form-group"> <p> 名前を付けて保存: <button class="btn btn-default" (click)="export('png')">PNG</button> <button class="btn btn-default" (click)="export('jpeg')">JPEG</button> <button class="btn btn-default" (click)="export('svg')">SVG</button> </p> <wj-flex-chart #theChart header="フェーズ2 テスト結果" chartType="Scatter"> <wj-flex-chart-series name="実験1" [itemsSource]="data1" bindingX="x" binding="y"></wj-flex-chart-series> <wj-flex-chart-series name="実験2" [itemsSource]="data2" bindingX="x" binding="y"></wj-flex-chart-series> <wj-flex-chart-series name="実験3" [itemsSource]="data3" bindingX="x" binding="y"></wj-flex-chart-series> <wj-flex-chart-axis wjProperty="axisY" [axisLine]="true"></wj-flex-chart-axis> </wj-flex-chart> </div> </div> import { Injectable } from '@angular/core'; // @Injectable() export class DataService { getData(cnt: number, a: number, b: number) { let arr = [], x = -5 * cnt / 2; // for (let i = 0; i < cnt; i++) { let rnd = Math.random() * cnt - cnt / 2; // arr.push({ x: x, y: a + x * (b + rnd) + rnd }); // x += .5 + Math.random() * 10; } // return arr; } } body { margin-bottom: 24px; } .wj-flexchart { height: 300px; margin: 10px 0; background: white; } label { margin-right: 3px; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './app.css'; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; // import * as Chart from '@grapecity/wijmo.react.chart'; import * as wjChart from '@grapecity/wijmo.chart'; import '@grapecity/wijmo.chart.render'; import { getData } from './data'; // class App extends React.Component { constructor(props) { super(props); this.state = { data1: getData(50, 0, 3), data2: getData(40, 100, 12), data3: getData(30, -100, 24) }; } render() { return <div className="container"> <div className="form-group"> <p> 名前を付けて保存: <button className="btn btn-default" onClick={this.exportChart.bind(this, "png")}>PNG</button> <button className="btn btn-default" onClick={this.exportChart.bind(this, "jpeg")}>JPEG</button> <button className="btn btn-default" onClick={this.exportChart.bind(this, "svg")}>SVG</button> </p> <div className="form-group"> <Chart.FlexChart header="フェーズ2 テスト結果" chartType="Scatter" initialized={this.chartInitialized.bind(this)}> <Chart.FlexChartSeries name="実験1" itemsSource={this.state.data1} bindingX="x" binding="y"> </Chart.FlexChartSeries> <Chart.FlexChartSeries name="実験2" itemsSource={this.state.data2} bindingX="x" binding="y"> </Chart.FlexChartSeries> <Chart.FlexChartSeries name="実験3" itemsSource={this.state.data3} bindingX="x" binding="y"> </Chart.FlexChartSeries> <Chart.FlexChartAxis wjProperty="axisY" axisLine={true}></Chart.FlexChartAxis> </Chart.FlexChart> </div> </div> </div>; } exportChart(type) { this.theChart.saveImageToFile('FlexChart.' + type); } chartInitialized(flex) { this.theChart = flex; } textChanged(s) { if (this.theChart) { this.theChart.dataLabel.position = s.text; } } showLinesAndBordersChanged() { this.setState({ showLinesAndBorders: !this.state.showLinesAndBorders }); if (this.theChart) { let dl = this.theChart.dataLabel; dl.connectingLine = dl.border = this.state.showLinesAndBorders; } } downloadsOnlyChanged() { if (this.theChart) { this.setState({ downloadsOnly: !this.state.downloadsOnly }); this.theChart.invalidate(); } } getRandomPalette() { let palettes = Object.getOwnPropertyNames(wjChart.Palettes) .filter(prop => typeof wjChart.Palettes[prop] === "object" && prop !== 'prototype'); let rand = Math.floor(Math.random() * palettes.length); // return wjChart.Palettes[palettes[rand]]; } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexChart Export</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> body { margin-bottom: 24px; } label { margin-right: 3px; } .wj-flexchart { background: white; } export function getData(cnt, a, b) { let arr = [], x = -5 * cnt / 2; // for (let i = 0; i < cnt; i++) { let rnd = Math.random() * cnt - cnt / 2; // arr.push({ x: x, y: a + x * (b + rnd) + rnd }); // x += .5 + Math.random() * 10; } // return arr; } <template> <div class="container-fluid"> <div class="form-group"> <p> 名前を付けて保存: <button class="btn btn-default" v-on:click="exportChart('png')">PNG</button> <button class="btn btn-default" v-on:click="exportChart('jpeg')">JPEG</button> <button class="btn btn-default" v-on:click="exportChart('svg')">SVG</button> </p> <wj-flex-chart header="フェーズ2 テスト結果" chartType="Scatter" :initialized="chartInitialized"> <wj-flex-chart-series name="実験1" :itemsSource="data1" bindingX="x" binding="y"></wj-flex-chart-series> <wj-flex-chart-series name="実験2" :itemsSource="data2" bindingX="x" binding="y"></wj-flex-chart-series> <wj-flex-chart-series name="実験3" :itemsSource="data3" bindingX="x" binding="y"></wj-flex-chart-series> <wj-flex-chart-axis wjProperty="axisY" :axisLine="true"></wj-flex-chart-axis> </wj-flex-chart> </div> </div> </template> <script> import "@grapecity/wijmo.styles/wijmo.css"; import 'bootstrap.css'; import Vue from 'vue'; import '@grapecity/wijmo.vue2.core'; import '@grapecity/wijmo.vue2.chart'; import { getData } from './data'; import '@grapecity/wijmo.chart.render'; let App = Vue.extend({ name: 'app', data: function () { return { data1: getData(50, 0, 3), data2: getData(40, 100, 12), data3: getData(30, -100, 24) } }, methods: { chartInitialized: function(flex) { this.theChart = flex; }, exportChart(type) { this.theChart.saveImageToFile('FlexChart.' + type); } } }) new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> body { margin-bottom: 24px; } .wj-flexchart { height: 300px; margin: 10px 0; background: white; } label { margin-right: 3px; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexChart Export</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"></div> </body> </html> export function getData(cnt, a, b) { let arr = [], x = -5 * cnt / 2; // for (let i = 0; i < cnt; i++) { let rnd = Math.random() * cnt - cnt / 2; // arr.push({ x: x, y: a + x * (b + rnd) + rnd }); // x += .5 + Math.random() * 10; } // return arr; }