Custom Line Segments

The sample shows customizing the line chart segments using itemFormatter.

The FlexChart's itemFormatter property allows you to specify a callback invoked when chart items are rendered. The callback may customize the item or add new ones using the chart engine's properties and methods.

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: 'Sales' }, { binding: 'expenses', name: 'Expenses' }, { binding: 'downloads', name: 'Downloads', chartType: 'Line' } ], itemFormatter: (engine, hitTestInfo, defaultFormat) => { let ht = hitTestInfo, binding = 'downloads'; // // check that this is the right series/element if (ht.series.binding == binding && ht.pointIndex > 0 && ht.chartElement == wjChart.ChartElement.SeriesSymbol) { // // get current and previous values let chart = ht.series.chart, items = chart.collectionView.items, valNow = items[ht.pointIndex][binding], valPrev = items[ht.pointIndex - 1][binding]; // // add line if value is increasing if (valNow > valPrev) { let pt1 = chart.dataToPoint(ht.pointIndex, valNow), pt2 = chart.dataToPoint(ht.pointIndex - 1, valPrev); // engine.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, null, { stroke: 'gold', strokeWidth: 6 }); } } // // render element as usual defaultFormat(); } }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexChart Custom Line Segments</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 = ['US', 'Canada', 'Mexico', 'Germany', 'UK', 'France', 'Japan', 'Korea', 'China'], 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; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wjChart from '@grapecity/wijmo.chart'; import { Component, Inject, enableProdMode, NgModule } 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'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any; // constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(); } // itemFormatter(engine: wjChart.IRenderEngine, ht: wjChart.HitTestInfo, defaultFormat: Function) { let binding = 'downloads'; // // check that this is the right series/element if (ht.series.binding == binding && ht.pointIndex > 0 && ht.chartElement == wjChart.ChartElement.SeriesSymbol) { // // get current and previous values let chart = ht.series.chart, items = chart.collectionView.items, valNow = items[ht.pointIndex][binding], valPrev = items[ht.pointIndex - 1][binding]; // // add line if value is increasing if (valNow > valPrev) { let pt1 = chart.dataToPoint(ht.pointIndex, valNow), pt2 = chart.dataToPoint(ht.pointIndex - 1, valPrev); // engine.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, null, { stroke: 'gold', strokeWidth: 6 }); } } // // render element as usual defaultFormat(); } } // @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 FlexGrid Custom Line Segments</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"> <wj-flex-chart [itemsSource]="data" [bindingX]="'country'" [itemFormatter]="itemFormatter"> <wj-flex-chart-series [binding]="'sales'" [name]="'Sales'"></wj-flex-chart-series> <wj-flex-chart-series [binding]="'expenses'" [name]="'Expenses'"></wj-flex-chart-series> <wj-flex-chart-series [binding]="'downloads'" [name]="'Downloads'" [chartType]="'Line'"></wj-flex-chart-series> </wj-flex-chart> </div> import { Injectable } from '@angular/core'; // @Injectable() export class DataService { getData() { let countries = ['US', 'Canada', 'Mexico', 'Germany', 'UK', 'France', 'Japan', 'Korea', 'China'], 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; } <template> <div class="container-fluid"> <wj-flex-chart :itemsSource="data" bindingX="country" :itemFormatter="itemFormatter"> <wj-flex-chart-series binding="sales" name="Sales"></wj-flex-chart-series> <wj-flex-chart-series binding="expenses" name="Expenses"></wj-flex-chart-series> <wj-flex-chart-series binding="downloads" name="Downloads" chartType="Line"></wj-flex-chart-series> </wj-flex-chart> </div> </template> <script> import '@grapecity/wijmo.styles/wijmo.css'; import 'bootstrap.css'; import Vue from 'vue'; import * as wjChart from '@grapecity/wijmo.chart'; import '@grapecity/wijmo.vue2.chart'; import { getData } from './data'; // new Vue({ el: '#app', data: { data: getData() }, methods: { itemFormatter(engine, ht, defaultFormat) { let binding = 'downloads'; // // check that this is the right series/element if (ht.series.binding == binding && ht.pointIndex > 0 && ht.chartElement == wjChart.ChartElement.SeriesSymbol) { // // get current and previous values let chart = ht.series.chart, items = chart.collectionView.items, valNow = items[ht.pointIndex][binding], valPrev = items[ht.pointIndex - 1][binding]; // // add line if value is increasing if (valNow > valPrev) { let pt1 = chart.dataToPoint(ht.pointIndex, valNow), pt2 = chart.dataToPoint(ht.pointIndex - 1, valPrev); // engine.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, null, { stroke: 'gold', strokeWidth: 6 }); } } // // render element as usual defaultFormat(); } } }); </script> <style> .container-fluid .wj-flexchart { height: 300px; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexChart Custom Line Segments</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> // create some random data export function getData() { let countries = ['US', 'Canada', 'Mexico', 'Germany', 'UK', 'France', 'Japan', 'Korea', 'China'], 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; } 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 wjChart from '@grapecity/wijmo.react.chart'; import * as chart from '@grapecity/wijmo.chart'; import { getData } from './data'; // class App extends React.Component { constructor(props) { super(props); this.itemFormatter = (engine, ht, defaultFormat) => { let binding = 'downloads'; // // check that this is the right series/element if (ht.series.binding == binding && ht.pointIndex > 0 && ht.chartElement == chart.ChartElement.SeriesSymbol) { // // get current and previous values let chart = ht.series.chart, items = chart.collectionView.items, valNow = items[ht.pointIndex][binding], valPrev = items[ht.pointIndex - 1][binding]; // // add line if value is increasing if (valNow > valPrev) { let pt1 = chart.dataToPoint(ht.pointIndex, valNow), pt2 = chart.dataToPoint(ht.pointIndex - 1, valPrev); // engine.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, null, { stroke: 'gold', strokeWidth: 6 }); } } // // render element as usual defaultFormat(); }; this.state = { data: getData() }; } render() { return <div className="container-fluid"> <wjChart.FlexChart itemsSource={this.state.data} bindingX="country" itemFormatter={this.itemFormatter}> <wjChart.FlexChartSeries binding="sales" name="Sales"> </wjChart.FlexChartSeries> <wjChart.FlexChartSeries binding="expenses" name="Expenses"> </wjChart.FlexChartSeries> <wjChart.FlexChartSeries binding="downloads" name="Downloads" chartType="Line"> </wjChart.FlexChartSeries> </wjChart.FlexChart> </div>; } } 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>AutoComplete</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-fluid .wj-flexchart { height: 300px; } export function getData() { let countries = ['US', 'Canada', 'Mexico', 'Germany', 'UK', 'France', 'Japan', 'Korea', 'China'], 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; }