linechart-demo.component.ts 4.7 KB
Newer Older
David Anderson's avatar
David Anderson committed
1
/* cSpell:words lcdp */
2
import { CanvasStyle, LineChartData, LineChartConfig, ContinuousAxisConfig, GistChart, DataPoint,GetDataAtMouseEvent } from 'gist-charts';
David Anderson's avatar
David Anderson committed
3
4
5
6
import { Component, ViewChild, ElementRef } from '@angular/core';

let pointCount = 40;

7
8
let lineCount = 1;
let stepCount = 1;
David Anderson's avatar
David Anderson committed
9
10
11
let spikeChance = 0.2;
let spikeJump = 5;

David Anderson's avatar
David Anderson committed
12
13
14
15
16
17

@Component({
  selector: 'app-linechart-demo',
  templateUrl: './linechart-demo.component.html',
  styleUrls: ['./linechart-demo.component.css']
})
David Anderson's avatar
David Anderson committed
18
19
20
21
22
23
export class LineChartDemoComponent  {
  @ViewChild( 'drawspace' )
  drawspace: ElementRef;
  intervals: number[] = [];

  ngAfterViewInit() {
24
      console.clear();
David Anderson's avatar
David Anderson committed
25
26
27
28
29
30
31
      let me = this;
      let leftAxis: ContinuousAxisConfig = new ContinuousAxisConfig( 'left' );
      let bottomAxis: ContinuousAxisConfig = new ContinuousAxisConfig( 'bottom' );

      let gistChart = new GistChart( me.drawspace.nativeElement );

      let activeChart = new LineChartConfig();
32

David Anderson's avatar
David Anderson committed
33
34
    //    gistChart.debugLogger.isEnabled = true;
    //    gistChart.debugLogger.logPrefix = 'LineDemo gistChart';
35

David Anderson's avatar
David Anderson committed
36
37
    //   activeChart.debugLogger.isEnabled = true;
    //   activeChart.debugLogger.logPrefix = 'lineDemo activeChart';
38

David Anderson's avatar
David Anderson committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
      activeChart.getLineStyle = ( line: LineChartData<LineData> ) => {
          let ret = new CanvasStyle();
          let data = line.data;
          if( !data ){
              return ret;
          }
          ret.stroke = data.stroke;
          ret.lineWidth = data.lineWidth;
          ret.lineDash = data.lineDash;

          return ret;
      };
      activeChart.datalist( [] );


      gistChart.background = '#ccc';

      leftAxis.shouldDrawGridLines = true;
      bottomAxis.shouldDrawGridLines = true;
      // bottomAxis.tickAngle = Math.PI * 0.25;
      // bottomAxis.tickTextAlign = 'left';
      // bottomAxis.tickTextAlign = 'right';

      bottomAxis.explicitMinValue = 0;
      bottomAxis.explicitMaxValue = pointCount;
      bottomAxis.tickSkipSteps = [3,4,5];

66
67
      bottomAxis.getValue = ( dp ) => { return dp.data.x; };
      leftAxis.getValue = ( dp ) => { return dp.data.y; };
David Anderson's avatar
David Anderson committed
68
69
70

      bottomAxis.valueToString = ( value ) => { return value.toString() };
      leftAxis.valueToString = ( value ) => { return value.toString(); };
David Anderson's avatar
David Anderson committed
71

David Anderson's avatar
David Anderson committed
72
73
74
75
76
77
78
79
80
81
82
83
84
      leftAxis.label = 'y axis';
      bottomAxis.label = 'x axis';

      leftAxis.registerChartConfig( activeChart );
      bottomAxis.registerChartConfig( activeChart );
      gistChart.addChart( activeChart );
      gistChart.addAxis( [
          leftAxis
          , bottomAxis
      ] );
      let minValue = Math.pow( 10, 2 );
      leftAxis.explicitMinValue = minValue;
      leftAxis.explicitMaxValue = minValue * 2;
85
86
87
88
89
90
91
92
93
94
95
96
97
98



      gistChart.addBehavior( new GetDataAtMouseEvent(
          'mousemove',
          {
              config: activeChart,
              onEvent: ( dpl: DataPoint[], config: LineChartConfig ) => {
                  let first = dpl[ 0 ];
                  console.log( first );
              }
          } as any
      ) );

David Anderson's avatar
David Anderson committed
99
100
101
102
103
104
105
106
107
      gistChart.renderAll();
      function draw() {
          activeChart.datalist().push( ...randomLineData( leftAxis.explicitMinValue as number, leftAxis.explicitMaxValue as number ) );
          if ( activeChart.datalist().length < lineCount ) {
              window.requestAnimationFrame( draw );
          }
      }

      draw();
David Anderson's avatar
David Anderson committed
108
109

  }
David Anderson's avatar
David Anderson committed
110
111
112
113
114
115
116
117
  ngOnDestroy() {
      this.intervals.forEach( int => {
          window.clearInterval( int );
      } );
  }
}

function randomLineData( min: number, max: number ): Array<LineChartData<LineData>> {
118
  let ret: Array<LineChartData<LineData>> = [];
119
  while ( ret.length < Math.min( stepCount, 100 )) {
David Anderson's avatar
David Anderson committed
120
121
122
123
124
125
126
      let lcdp = new LineChartData();
      lcdp.data = {
          lineDash: ret.length % 2 ? [ 5, 15 ] : undefined,
          lineWidth: randomInt( 1, 5 ),
          stroke: 'rgb(' + randomInt( 0, 255 ) + ',' + randomInt( 0, 255 ) + ',' + randomInt( 0, 255 ) + ' )'
      };
      while ( lcdp.points.length < pointCount ) {
127
          let dp = new DataPoint({
David Anderson's avatar
David Anderson committed
128
129
              x: lcdp.points.length + 1,
              y: Math.random() * ( max - min ) + min
130
          });
David Anderson's avatar
David Anderson committed
131
132

          if ( Math.random() < spikeChance ) {
133
              dp.data.y = max * spikeJump;
David Anderson's avatar
David Anderson committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
          }

          // use this to create an x on the chart. useful for debugging
          // if ( ret.length % 2 === 0 ) {
          //     dp.y = lcdp.points.length;
          // } else {
          //     dp.y = pointCount - lcdp.points.length -1;
          // }

          lcdp.points.push( dp );
      }
      ret.push( lcdp );
  }

  return ret;
}

function randomInt( min: number, max: number ): number {
  let diff = max - min;
  return Math.round( Math.random() * diff + min );
}
David Anderson's avatar
David Anderson committed
155

156
class LineData  extends DataPoint{
David Anderson's avatar
David Anderson committed
157
158
159
  lineDash: number[] | undefined;
  lineWidth: number = 0;
  stroke: string = '';
David Anderson's avatar
David Anderson committed
160
}