chartjs.component.ts 8.05 KB
Newer Older
Ooh-Ao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
import { Component, ElementRef, ViewChild } from '@angular/core';
import * as Chart from 'chart.js';
import * as chartData from 'src/app/shared/data/charts/chartjs';
import {ChartConfiguration  ,ChartData, ChartEvent, ChartOptions, ChartType } from 'chart.js';

@Component({
  selector: 'app-chartjs',
  templateUrl: './chartjs.component.html',
  styleUrls: ['./chartjs.component.scss'],
})
export class ChartjsComponent {
  @ViewChild('Canvas') Canvas: ElementRef | any;
  @ViewChild('myCanvas') myCanvas: ElementRef | any;

  constructor() {}

  ngOnInit(): void {}

  ngAfterViewInit() {
    //gradient bar chart
    if (this.myCanvas && this.myCanvas.nativeElement) {
      const ctx: CanvasRenderingContext2D =
        this.myCanvas.nativeElement.getContext('2d');
      const canvas: any = (<HTMLCanvasElement>(
        this.Canvas.nativeElement
      )).getContext('2d');
      const gradient = canvas.createLinearGradient(0, 0, 0, 250);
      gradient.addColorStop(0, '#285cf7');
      gradient.addColorStop(1, '#f7557a');
      new Chart.Chart(canvas, {
        type: 'bar',
        data: {
          labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
          datasets: [
            {
              label: '# of Votes',
              data: [12, 39, 20, 10, 25, 18],
              backgroundColor: gradient,
              barPercentage: 0.6,
            },
          ],
        },
        options: {
          maintainAspectRatio: false,
          responsive: true,
          plugins: {
            legend: {
              display: false,
            },
          },
          scales: {
            y: {
              max: 80,
              ticks: {
                color: 'rgba(171, 167, 167,0.9)',
              },
              grid: {
                display: true,
                color: 'rgba(171, 167, 167,0.2)',
              },
            },
            x: {
              ticks: {
                color: 'rgba(171, 167, 167,0.9)',
              },
              grid: {
                display: true,
                color: 'rgba(171, 167, 167,0.2)',
              },
            },
          },
        },
      });
      //Area Chart
      const areaGradient1 = ctx.createLinearGradient(0, 350, 0, 0);
      areaGradient1.addColorStop(0, 'rgba(247, 85, 122, 0)');
      areaGradient1.addColorStop(1, 'rgba(247, 85, 122, .5)');

      const areaGradient2 = ctx.createLinearGradient(0, 280, 0, 0);
      areaGradient2.addColorStop(0, 'rgba(0, 123, 255, 0)');
      areaGradient2.addColorStop(1, 'rgba(0, 123, 255, .3)');
      const areachart = new Chart.Chart(ctx, {
        type: 'line',
        data: {
          labels: [
            'Jan',
            'Feb',
            'Mar',
            'Apr',
            'May',
            'Jun',
            'July',
            'Aug',
            'Sep',
            'Oct',
            'Nov',
            'Dec',
          ],
          datasets: [
            {
              data: [12, 15, 18, 40, 35, 38, 32, 20, 25, 15, 25, 30],
              borderColor: '#f7557a',
              borderWidth: 1,
              backgroundColor: areaGradient1,
              tension: 0.4,
              fill: 'origin',
            },
            {
              data: [10, 20, 25, 55, 50, 45, 35, 37, 45, 35, 55, 40],
              borderColor: '#007bff',
              borderWidth: 1,
              backgroundColor: areaGradient2,
              tension: 0.4,
              fill: 'origin',
            },
          ],
        },
        options: {
          maintainAspectRatio: false,
          plugins: {
            legend: {
              display: false,
            },
          },
          scales: {
            y: {
              ticks: {
                color: 'rgba(171, 167, 167,0.9)',
              },
              grid: {
                display: true,
                color: 'rgba(171, 167, 167,0.2)',
              },
            },
            x: {
              ticks: {
                color: 'rgba(171, 167, 167,0.9)',
              },
              grid: {
                display: true,
                color: 'rgba(171, 167, 167,0.2)',
              },
            },
          },
        },
      });
    }
  }

  //Line Chart
  public lineChartOptions = chartData.lineChartOptions;
  public lineChartType = chartData.lineChartType;
  public lineChartLegend = chartData.lineChartLegend;
  public lineChartData = chartData.lineChartData;

  //Doughnut and Pie Chart Data
  public PieChartData = chartData.PieChartData;
  public PieChartOptions = chartData.PieChartOptions;
  public PieChartType = chartData.PieChartType;
  public DoughnutChartType = chartData.DoughnutChartType;

  //Bar Chart (Vertical)
  public barChart2Options = chartData.barChart2Options;
  public barChart2Type = chartData.barChart2Type;
  public barChart2Legend = chartData.barChart2Legend;
  public barChart2Data = chartData.barChart2Data;

  // Solid Color
  public solidColorOptions = chartData.solidColorChartOptions;
  public solidColorType = chartData.solidColorChartType;
  public solidColorData = chartData.solidColorChartData;
  public solidColorLegend = chartData.solidColorLegend;

  //polar
  public polarAreaChartLabels = chartData.polarAreaChartLabels;
  public polarAreaChartData = chartData.polarAreaChartData;
  public polarAreaLegend = chartData.polarAreaLegend;
  public polarChartOptions = chartData.polarChartOptions;
  public polarAreaChartType = chartData.polarAreaChartType;

  public scatterChartLabels: string[] = [
    'Eating',
    'Drinking',
    'Sleeping',
    'Designing',
    'Coding',
    'Cycling',
    'Running',
  ];
  public scatterChartOptions: ChartConfiguration['options'] = {
    scales: {
      x: {
        grid: {
          display: true,
          color: 'rgba(171, 167, 167,0.2)',
        },
      },
      y: {
        grid: {
          display: true,
          color: 'rgba(171, 167, 167,0.2)',
        },
      },
    },
  };

  public scatterChartData: ChartData<'scatter'> = {
    labels: this.scatterChartLabels,
    datasets: [
      {
        data: [
          { x: 1, y: 1 },
          { x: 2, y: 3 },
          { x: 3, y: -2 },
       
        ],
        label: 'Series A',
        pointRadius: 10,
        backgroundColor: '#6366f1',
      },
    ],
  };
  public scatterChartType: ChartType = 'scatter';
  public bubbleChartOptions: ChartConfiguration['options'] = {
    scales: {
      x: {
        min: 0,
        max: 30,
        ticks: {},
        grid: {
          display: true,
          color: 'rgba(171, 167, 167,0.2)',
        },
      },
      y: {
        min: 0,
        max: 30,
        ticks: {},
        grid: {
          display: true,
          color: 'rgba(171, 167, 167,0.2)',
        },
      },
    },
  };
  public bubbleChartType: ChartType = 'bubble';
  public bubbleChartLegend = true;

  public bubbleChartData: ChartData<'bubble'> = {
    labels: [],
    datasets: [
      {
        data: [
          { x: 10, y: 10, r: 10 },
          { x: 15, y: 5, r: 15 },
        ],
        label: 'First Dataset',
        backgroundColor: ['#6366f1'],
        borderColor: 'blue',
        hoverBackgroundColor: 'purple',
        hoverBorderColor: '#6366f1',
      },
    ],
  };

  // Pie
  public pieChartOptions: ChartOptions<'pie'> = {
    responsive: true,
  };
  public pieChartLabels = ['Priamry', 'Success', 'Warning'];
  public pieChartDatasets = [
    {
      data: [300, 500, 100],
      backgroundColor: [
        'rgba(99, 102, 241, 1)', // Primary color
        'rgba(94, 166, 142, 1)', // Success color
        'rgba(166, 142, 94, 1)', // Warning color
      ],
    },
  ];
  public pieChartLegend = true;
  public pieChartPlugins = [];

  // Doughnut
  public doughnutChartLabels: string[] = ['primary', 'success', 'warning'];
  public doughnutChartDatasets: ChartConfiguration<'doughnut'>['data']['datasets'] =
    [
      {
        data: [350, 450, 100],
        backgroundColor: [
          'rgba(99, 102, 241, 1)', // Primary color
          'rgba(94, 166, 142, 1)', // Success color
          'rgba(166, 142, 94, 1)', // Warning color
        ],
        label: 'Series A',
      },
    ];

  public doughnutChartOptions: ChartConfiguration<'doughnut'>['options'] = {
    responsive: true,
  };
}