Skip to content
Snippets Groups Projects
Commit 92263412 authored by Xiao Gui's avatar Xiao Gui Committed by xgui3783
Browse files

allow for download of line graph as csv

parent 3a061584
No related branches found
No related tags found
No related merge requests found
import { Component, Input, OnChanges, ElementRef, ViewChild } from '@angular/core' import { Component, Input, OnChanges, ElementRef, ViewChild } from '@angular/core'
import { DatasetInterface, ChartColor, ScaleOptionInterface, LegendInterface, TitleInterfacce, applyOption } from '../chart.interface' import { DatasetInterface, ChartColor, ScaleOptionInterface, LegendInterface, TitleInterfacce, applyOption } from '../chart.interface'
import { ChartOptions, LinearTickOptions,ChartDataSets } from 'chart.js'; import { ChartOptions, LinearTickOptions,ChartDataSets, ChartPoint } from 'chart.js';
import { Color } from 'ng2-charts'; import { Color } from 'ng2-charts';
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
@Component({ @Component({
selector : `line-chart`, selector : `line-chart`,
templateUrl : './line.chart.template.html', templateUrl : './line.chart.template.html',
...@@ -11,6 +12,7 @@ import { Color } from 'ng2-charts'; ...@@ -11,6 +12,7 @@ import { Color } from 'ng2-charts';
], ],
}) })
export class LineChart implements OnChanges{ export class LineChart implements OnChanges{
@ViewChild('canvas') canvas : ElementRef @ViewChild('canvas') canvas : ElementRef
/** /**
...@@ -102,6 +104,10 @@ export class LineChart implements OnChanges{ ...@@ -102,6 +104,10 @@ export class LineChart implements OnChanges{
shapedLineChartDatasets : ChartDataSets[] shapedLineChartDatasets : ChartDataSets[]
constructor(private sanitizer:DomSanitizer){
}
ngOnChanges(){ ngOnChanges(){
this.shapedLineChartDatasets = this.lineDatasets.map(lineDataset=>({ this.shapedLineChartDatasets = this.lineDatasets.map(lineDataset=>({
data : lineDataset.data.map((v,idx)=>({ data : lineDataset.data.map((v,idx)=>({
...@@ -120,11 +126,37 @@ export class LineChart implements OnChanges{ ...@@ -120,11 +126,37 @@ export class LineChart implements OnChanges{
},0) },0)
applyOption(this.chartOption,this.options) applyOption(this.chartOption,this.options)
}
getDataPointString(input:any):string{
return typeof input === 'number' || typeof input === 'string'
? input.toString()
: this.getDataPointString(input.y)
}
// this.colors = get cvsData():string{
// this.colors = [{ const row0 = [this.chartOption.scales.xAxes[0].scaleLabel.labelString].concat(this.chartOption.scales.yAxes[0].scaleLabel.labelString).join(',')
// backgroundColor : 'rgba(0,255,0,0.2)' const maxRows = this.lineDatasets.reduce((acc, lds) => Math.max(acc, lds.data.length), 0)
// }] const rows = Array.from(Array(maxRows)).map((_,idx) => [idx.toString()].concat(this.shapedLineChartDatasets.map(v => v.data[idx] ? this.getDataPointString(v.data[idx]) : '').join(','))).join('\n')
return `${row0}\n${rows}`
}
get csvDataUrl():SafeUrl{
return this.sanitizer.bypassSecurityTrustUrl(`data:text/csv;charset=utf-8,${this.cvsData}`)
}
get csvTitle(){
return `${this.graphTitleAsString.replace(/\s/g, '')}.csv`
}
get graphTitleAsString():string{
try{
return this.chartOption.title.text.constructor === Array
? (this.chartOption.title.text as string[]).join(' ')
: this.chartOption.title.text as string
}catch(e){
return `Untitled`
}
} }
} }
......
...@@ -11,6 +11,9 @@ ...@@ -11,6 +11,9 @@
[labels]="chartDataset.labels" [labels]="chartDataset.labels"
#canvas> #canvas>
</canvas> </canvas>
<a [download] = "csvTitle" [href] = "csvDataUrl" *ngIf = "shapedLineChartDatasets">
download line graph as csv
</a>
<span <span
*ngIf = "!shapedLineChartDatasets"> *ngIf = "!shapedLineChartDatasets">
datasets are required to display linear graph datasets are required to display linear graph
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment