チャート:複数の軸

ほとんどのチャートにはX軸とY軸の2つの軸があります。これは、チャート上のすべてのデータが同じ性質を持ち、同じスケールを共有できる限り、うまく機能します。

しかし、いくつかのチャートには、さまざまな種類のデータを表示する系列があり、単位やスケールが異なります。以下の図は、金額(売上と費用)を表す2つの系列と、数量(DL数)を表す1つの系列です。

1つのY軸に対してすべての系列をプロットすると、最初の2つの系列がグラフの最下部に向かって押し込められてしまいます。

この問題を解決して単一のチャートを使用する最も簡単な方法は、2つ目のY軸を作成し、それを「DL数」系列のaxisYプロパティに割り当てることです。

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as wjChart from '@grapecity/wijmo.chart'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // create the chart let theChart = new wjChart.FlexChart('#theChart', { itemsSource: getData(), bindingX: 'country', series: [ { binding: 'sales', name: '売上' }, { binding: 'expenses', name: '費用' }, { binding: 'downloads', name: 'DL数', chartType: 'LineSymbols' } ], axisY: { format: 'n0,', title: '売上/費用(千ドル)', axisLine: true }, }); theChart.palette = getRandomPalette(); // // create and apply extra Y axis for 'Downloads' series let axisY2 = new wjChart.Axis(); axisY2.position = wjChart.Position.Right; axisY2.title = 'DL数(千)'; axisY2.format = 'n0,'; axisY2.min = 0; axisY2.axisLine = true; getSeries('downloads').axisY = axisY2; // // toggle extra axis document.querySelector('#secondaryAxis').addEventListener('click', e => { getSeries('downloads').axisY = e.target.checked ? axisY2 : null; }); // // get a series by its binding function getSeries(binding) { let s = theChart.series; // for (let i = 0; i < s.length; i++) { if (s[i].binding == binding) { return s[i]; } } // return null; } } // function 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]]; } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexChart Multiple Axes</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"> <label for="secondaryAxis">2つ目のY軸</label> <input id="secondaryAxis" type="checkbox" checked="checked"> <div id="theChart"></div> </div> </body> </html> // create some random data export function getData() { let countries = 'アメリカ,カナダ,メキシコ,ドイツ,イギリス,フランス,イタリア,日本,韓国,中国'.split(','), data = []; // for (let i = 0; i < countries.length; i++) { data.push({ country: countries[i], sales: 1000 + Math.random() * 10000, expenses: 1000 + Math.random() * 5000, downloads: Math.round(Math.random() * 100000) }); } // return data; } .wj-flexchart { height: 300px; } .wj-control { margin-bottom: 6px; } 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 { FormsModule } from '@angular/forms'; import { WjChartModule } from '@grapecity/wijmo.angular2.chart'; import { DataService } from './app.data'; import * as wjChart from '@grapecity/wijmo.chart'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any[]; palette: any; @ViewChild('axisY') axisY: wjChart.Axis; @ViewChild('ser') ser: wjChart.Series; // constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(); this.palette = this.getRandomPalette(); } // 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]]; } // saChange(sa: HTMLInputElement) { this.ser.axisY = sa.checked ? this.axisY : null; } } // @NgModule({ imports: [FormsModule, 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 Multiple Axes</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"> <label for="secondaryAxis">2つ目のY軸</label> <input #sa id="secondaryAxis" type="checkbox" checked="checked" (change)="saChange(sa)"> <wj-flex-chart bindingX="country" [itemsSource]="data" [palette]="palette"> <wj-flex-chart-series binding="sales" name="売上"></wj-flex-chart-series> <wj-flex-chart-series binding="expenses" name="費用"></wj-flex-chart-series> <wj-flex-chart-series #ser binding="downloads" name="DL数" chartType="LineSymbols"> <wj-flex-chart-axis #axisY wjProperty="axisY" position="Right" title="DL数(千)" format="n0," [min]="0" [axisLine]="true"></wj-flex-chart-axis> </wj-flex-chart-series> <wj-flex-chart-axis wjProperty="axisY" format="n0," title="売上/費用(千ドル)" [axisLine]="true"></wj-flex-chart-axis> </wj-flex-chart> </div> </div> import { Injectable } from '@angular/core'; // @Injectable() export class DataService { getData() { let countries = 'アメリカ,カナダ,メキシコ,ドイツ,イギリス,フランス,イタリア,日本,韓国,中国'.split(','), data = []; // for (let i = 0; i < countries.length; i++) { data.push({ country: countries[i], sales: 1000 + Math.random() * 10000, expenses: 1000 + Math.random() * 5000, downloads: Math.round(Math.random() * 100000) }); } // return data; } } body { margin-bottom: 24px; } label { margin-right: 3px; } import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; import "./app.css"; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; // import * as wjChart from "@grapecity/wijmo.react.chart"; import * as wjcChart from "@grapecity/wijmo.chart"; import { getData } from "./data"; class App extends React.Component { constructor(props) { super(props); this.state = { data: getData(), palette: this.getRandomPalette() }; } render() { return <div className="container-fluid"> <div className="form-group"> <label htmlFor="secondaryAxis">2つ目のY軸</label> <input id="secondaryAxis" type="checkbox" defaultChecked onClick={this.saChange.bind(this)}/> <wjChart.FlexChart bindingX="country" initialized={this.initializeChart.bind(this)} itemsSource={this.state.data} palette={this.state.palette}> <wjChart.FlexChartSeries binding="sales" name="売上"></wjChart.FlexChartSeries> <wjChart.FlexChartSeries binding="expenses" name="費用"></wjChart.FlexChartSeries> <wjChart.FlexChartSeries binding="downloads" name="DL数" chartType="LineSymbols"> <wjChart.FlexChartAxis wjProperty="axisY" position="Right" title="DL数(千)" format="n0," min={0} axisLine={true}> </wjChart.FlexChartAxis> </wjChart.FlexChartSeries> <wjChart.FlexChartAxis wjProperty="axisY" format="n0," title="売上/費用(千ドル)" axisLine={true}> </wjChart.FlexChartAxis> </wjChart.FlexChart> </div> </div>; } initializeChart(flex) { this.ser = flex.series[2]; this.axisY = this.ser.axisY; } getRandomPalette() { let palettes = Object.getOwnPropertyNames(wjcChart.Palettes) .filter(prop => typeof wjcChart.Palettes[prop] === "object" && prop !== 'prototype'); let rand = Math.floor(Math.random() * palettes.length); // return wjcChart.Palettes[palettes[rand]]; } saChange(sa) { this.ser.axisY = sa.target.checked ? this.axisY : null; } } 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 OLAP Pivot Chart Overview</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; } export function getData() { let countries = 'アメリカ,カナダ,メキシコ,ドイツ,イギリス,フランス,イタリア,日本,韓国,中国'.split(','), data = []; // for (let i = 0; i < countries.length; i++) { data.push({ country: countries[i], sales: 1000 + Math.random() * 10000, expenses: 1000 + Math.random() * 5000, downloads: Math.round(Math.random() * 100000) }); } // return data; } <template> <div class="container-fluid"> <div class="form-group"> <label for="secondaryAxis">2つ目のY軸</label> <input id="secondaryAxis" type="checkbox" checked="checked" v-on:change="saChange"> <wj-flex-chart bindingX="country" :itemsSource="data" :palette="palette" :initialized="initializeChart"> <wj-flex-chart-series binding="sales" name="売上"></wj-flex-chart-series> <wj-flex-chart-series binding="expenses" name="費用"></wj-flex-chart-series> <wj-flex-chart-series binding="downloads" name="DL数" chartType="LineSymbols"> <wj-flex-chart-axis wjProperty="axisY" position="Right" title="DL数(千)" format="n0," :min="0" :axisLine="true"></wj-flex-chart-axis> </wj-flex-chart-series> <wj-flex-chart-axis wjProperty="axisY" format="n0," title="売上/費用(千ドル)" :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 * as wjChart from '@grapecity/wijmo.chart'; let App = Vue.extend({ name: 'app', data: function () { return { data: getData(), palette: this.getRandomPalette() } }, methods: { initializeChart: function(flex) { this.ser = flex.series[2]; this.axisY = this.ser.axisY; }, getRandomPalette: function() { 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]]; }, saChange: function(sa) { this.ser.axisY = sa.target.checked ? this.axisY : null; } } }) new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> body { margin-bottom: 24px; } 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 Multiple Axes</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() { let countries = 'アメリカ,カナダ,メキシコ,ドイツ,イギリス,フランス,イタリア,日本,韓国,中国'.split(','), data = []; // for (let i = 0; i < countries.length; i++) { data.push({ country: countries[i], sales: 1000 + Math.random() * 10000, expenses: 1000 + Math.random() * 5000, downloads: Math.round(Math.random() * 100000) }); } // return data; }