ヒットテスト

FlexChartは、そのオブジェクトモデルを使用してカスタマイズできる要素で構成されています。要素は次のとおりです。

  • PlotArea: 軸内の領域です。
  • ChartArea: コントロール内の、軸の外側の領域です。
  • Legend: グラフの凡例。通常はプロット領域の右側に表示されます。
  • Header: グラフヘッダー、プロット領域の上。
  • Footer: グラフフッター、プロット領域の下。
  • AxisX: X軸です。通常は水平です。
  • AxisY: Y軸です。通常は垂直です。
  • Series: チャート系列。
  • SeriesSymbol: チャート系列のシンボル。
  • DataLabel: データ点に付けられたラベル。

hitTestメソッドを使用して、マウスの下にあるグラフ領域を特定できます。

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as wjChart from '@grapecity/wijmo.chart'; import * as wjCore from '@grapecity/wijmo'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // create a chart let theChart = new wjChart.FlexChart('#theChart', { itemsSource: getData(), bindingX: 'country', chartType: 'LineSymbols', header: 'ヘッダー', footer: 'フッター', dataLabel: { content: '{value:n0}', position: 'Top', offset: 5 }, tooltip: { content: '' }, series: [ { binding: 'sales', name: '売上' }, { binding: 'expenses', name: '費用' } ] }); theChart.palette = getRandomPalette(); // // use tooltip to show hit-test information let tt = new wjCore.Tooltip(), tip = ''; // theChart.hostElement.addEventListener('mousemove', e => { // build tooltip text let ht = theChart.hitTest(e), elem = ht.chartElement, series = (ht.series && [1, 2, 3].indexOf(elem) < 0) ? ht.series : null, index = (ht.pointIndex != null && series) ? ht.pointIndex : null, newTip = wjCore.format('チャート要素: <b>{elem}</b><br/>系列: <b>{series}</b><br/>点のインデックス: <b>{index}</b>', { elem: wjChart.ChartElement[elem], series: series ? series.name : 'none', index: index != null ? index : 'none' }); // // update tooltip if (newTip != tip) { tip = newTip; tt.show(e.target, tip, new wjCore.Rect(e.clientX, e.clientY, 0, 0)); } }); // theChart.hostElement.addEventListener('mouseleave', e => { tt.hide(); tip = ''; }); } // 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 Chart Elements and Hit-Testing</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"> <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: Math.random() * 10000, expenses: Math.random() * 5000, downloads: Math.round(Math.random() * 20000) }); } // return data; } .wj-flexchart { height: 300px; } .wj-data-label { font-size: 75%; opacity: .9; } label { width: 150px; text-align: right; } .wj-control { margin-bottom: 3px; } body { margin-bottom: 24pt; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import { Component, Inject, enableProdMode, NgModule, ViewChild, AfterViewInit } 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 wjCore from '@grapecity/wijmo'; import * as wjChart from '@grapecity/wijmo.chart'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent implements AfterViewInit { data: any[]; palette: any; @ViewChild('theChart') theChart: wjChart.FlexChart; // constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(); this.palette = this.getRandomPalette(); } // ngAfterViewInit() { // use tooltip to show hit-test information let tt = new wjCore.Tooltip(), tip = ''; // this.theChart.hostElement.addEventListener('mousemove', e => { // build tooltip text let ht = this.theChart.hitTest(e), elem = ht.chartElement, series = (ht.series && [1, 2, 3].indexOf(elem) < 0) ? ht.series : null, index = (ht.pointIndex != null && series) ? ht.pointIndex : null, newTip = wjCore.format('チャート要素: <b>{elem}</b><br/>系列: <b>{series}</b><br/>点のインデックス: <b>{index}</b>', { elem: wjChart.ChartElement[elem], series: series ? series.name : 'none', index: index != null ? index : 'none' }); // // update tooltip if (newTip != tip) { tip = newTip; tt.show(e.target, tip, new wjCore.Rect(e.clientX, e.clientY, 0, 0)); } }); // this.theChart.hostElement.addEventListener('mouseleave', e => { tt.hide(); tip = ''; }); } // 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]]; } } // @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 Chart Elements and Hit-Testing</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"> <wj-flex-chart #theChart tooltipContent="" [itemsSource]="data" bindingX="country" chartType="LineSymbols" header="ヘッダー" footer="フッター" [palette]="palette"> <wj-flex-chart-data-label content="{value:n0}" position="Top" [offset]="5"></wj-flex-chart-data-label> <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> </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: Math.random() * 10000, expenses: Math.random() * 5000, downloads: Math.round(Math.random() * 20000) }); } // return data; } } .wj-flexchart { height: 300px; } .wj-data-label { font-size: 75%; opacity: .9; } label { width: 150px; text-align: right; } .wj-control { margin-bottom: 3px; } body { margin-bottom: 24pt; } 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 wjCore from '@grapecity/wijmo'; import * as wjChart from '@grapecity/wijmo.chart'; import * as Chart from '@grapecity/wijmo.react.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"> <div className="form-group"> <Chart.FlexChart tooltipContent="" header="ヘッダー" footer="フッター" chartType="LineSymbols" bindingX="country" palette={this.state.palette} itemsSource={this.state.data} initialized={this.chartInitialized.bind(this)}> <Chart.FlexChartDataLabel content="{value:n0}" position="Top" offset={5}></Chart.FlexChartDataLabel> <Chart.FlexChartSeries binding="sales" name="売上"></Chart.FlexChartSeries> <Chart.FlexChartSeries binding="expenses" name="費用"></Chart.FlexChartSeries> </Chart.FlexChart> </div> </div>; } chartInitialized(flex) { this.theChart = flex; this.theChart.tooltip.content = null; // use tooltip to show hit-test information let tt = new wjCore.Tooltip(), tip = ''; // this.theChart.hostElement.addEventListener('mousemove', e => { // build tooltip text let ht = this.theChart.hitTest(e), elem = ht.chartElement, series = (ht.series && [1, 2, 3].indexOf(elem) < 0) ? ht.series : null, index = (ht.pointIndex != null && series) ? ht.pointIndex : null, newTip = wjCore.format('チャート要素: <b>{elem}</b><br/>系列: <b>{series}</b><br/>点のインデックス: <b>{index}</b>', { elem: wjChart.ChartElement[elem], series: series ? series.name : 'none', index: index != null ? index : 'none' }); // // update tooltip if (newTip != tip) { tip = newTip; tt.show(e.target, tip, new wjCore.Rect(e.clientX, e.clientY, 0, 0)); } }); // this.theChart.hostElement.addEventListener('mouseleave', e => { tt.hide(); tip = ''; }); } 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 Chart Elements and Hit-Testing</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> .container .wj-flexchart { height: 300px; } .container .wj-data-label { font-size: 75%; opacity: .9; } .container label { width: 150px; text-align: right; } .container .wj-control { margin-bottom: 3px; } body { margin-bottom: 24pt; } export function getData() { let countries = 'アメリカ,ドイツ,イギリス,日本,イタリア,ギリシャ'.split(','), data = []; // for (let i = 0; i < countries.length; i++) { data.push({ country: countries[i], sales: Math.random() * 10000, expenses: Math.random() * 5000, downloads: Math.round(Math.random() * 20000) }); } // return data; } <template> <div class="container-fluid"> <div class="form-group"> <wj-flex-chart tooltipContent="" :itemsSource="data" bindingX="country" chartType="LineSymbols" header="ヘッダー" footer="フッター" :palette="palette" :initialized="chartInitialized"> <wj-flex-chart-data-label content="{value:n0}" position="Top" :offset="5"></wj-flex-chart-data-label> <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> </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 wjCore from '@grapecity/wijmo'; import * as wjChart from '@grapecity/wijmo.chart'; let App = Vue.extend({ name: 'app', data: function () { return { data: getData(), palette: this.getRandomPalette() } }, methods: { chartInitialized: function(flex) { this.theChart = flex; // use tooltip to show hit-test information let tt = new wjCore.Tooltip(), tip = ''; // this.theChart.hostElement.addEventListener('mousemove', e => { // build tooltip text let ht = this.theChart.hitTest(e), elem = ht.chartElement, series = (ht.series && [1, 2, 3].indexOf(elem) < 0) ? ht.series : null, index = (ht.pointIndex != null && series) ? ht.pointIndex : null, newTip = wjCore.format('チャート要素: <b>{elem}</b><br/>系列: <b>{series}</b><br/>点のインデックス: <b>{index}</b>', { elem: wjChart.ChartElement[elem], series: series ? series.name : 'none', index: index != null ? index : 'none' }); // // update tooltip if (newTip != tip) { tip = newTip; tt.show(e.target, tip, new wjCore.Rect(e.clientX, e.clientY, 0, 0)); } }); // this.theChart.hostElement.addEventListener('mouseleave', e => { tt.hide(); tip = ''; }); }, 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]]; } } }) new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> .container .wj-flexchart { height: 300px; } .container .wj-data-label { font-size: 75%; opacity: .9; } .container label { width: 150px; text-align: right; } .container .wj-control { margin-bottom: 3px; } body { margin-bottom: 24pt; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexChart Chart Elements and Hit-Testing</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: Math.random() * 10000, expenses: Math.random() * 5000, downloads: Math.round(Math.random() * 20000) }); } // return data; }