File

apps/live-weather/src/app/shared/place-hourly-forecast/place-hourly-forecast.component.ts

Implements

OnChanges

Metadata

selector weather-base-place-hourly-forecast
styleUrls ./place-hourly-forecast.component.scss
templateUrl ./place-hourly-forecast.component.html

Index

Properties
Methods
Inputs

Inputs

place
Type : IPlace
weather
Type : IOpenWeatherReport

Methods

Public formatWeatherReportForHourlyData
formatWeatherReportForHourlyData(weatherReport: IOpenWeatherReport)

Format weather report into chart specific data structure

Parameters :
Name Type Optional Description
weatherReport IOpenWeatherReport No

which is formatted for charts

Returns : IChart

chart data

ngOnChanges
ngOnChanges(changes: SimpleChanges)
Parameters :
Name Type Optional
changes SimpleChanges No
Returns : void

Properties

colorScheme
Type : object
Default value : { domain: ['#5AA454'], }
weatherData
Type : IChart[]
Default value : []
xAxisLabel
Type : string
Default value : 'Time'
yAxisLabel
Type : string
Default value : 'Temperature in (°C)'
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { IOpenWeatherReport } from '../models/IOpenWeatherReport';
import { IPlace } from '../models/IPlace';
import { IWeatherDetail } from '../models/IWeatherDetail';
import { IChart, ISeries } from './IChart';

@Component({
  selector: 'weather-base-place-hourly-forecast',
  templateUrl: './place-hourly-forecast.component.html',
  styleUrls: ['./place-hourly-forecast.component.scss'],
})
/**
 * Build hourly based charts based on weather report
 */
export class PlaceHourlyForecastComponent implements OnChanges {
  @Input() place!: IPlace;

  @Input() weather!: IOpenWeatherReport;

  weatherData: IChart[] = [];

  xAxisLabel = 'Time';

  yAxisLabel = 'Temperature in (°C)';

  colorScheme = {
    domain: ['#5AA454'],
  };

  /**
   * @internal
   */
  ngOnChanges(changes: SimpleChanges): void {
    if (changes.weather && changes.weather.currentValue) {
      this.weatherData = [this.formatWeatherReportForHourlyData(this.weather)];
      console.log(this.weatherData);
    }
  }

  /**
   * Format weather report into chart specific data structure
   *
   * @param weatherReport which is formatted for charts
   * @returns chart data
   */
  public formatWeatherReportForHourlyData(weatherReport: IOpenWeatherReport): IChart {
    const series = weatherReport.hourly.slice(0, 7).map(
      (weatherDetail: IWeatherDetail): ISeries => {
        return {
          name: `${new Date(weatherDetail.dt * 1000).getHours()}:00`,
          value: weatherDetail.temp,
        };
      }
    );

    return {
      name: this.place.name,
      series: series,
    };
  }
}
<div class="place-hourly-forecast-container mat-elevation-z11" fxLayout="column">
  <mat-toolbar
    fxLayout="row"
    fxLayoutAlign="start center"
    class="place-hourly-forecast-container__header"
    color="primary"
  >
    <span>Hourly forecast</span>
  </mat-toolbar>

  <ngx-charts-line-chart
    class="place-hourly-forecast-container__chart"
    [scheme]="colorScheme"
    legend="false"
    [showXAxisLabel]="true"
    [showYAxisLabel]="true"
    [xAxis]="true"
    [yAxis]="true"
    [xAxisLabel]="xAxisLabel"
    [yAxisLabel]="yAxisLabel"
    [timeline]="true"
    [results]="weatherData"
  >
  </ngx-charts-line-chart>
</div>

./place-hourly-forecast.component.scss

:host {
  width: 100%;
  background: white;

  .place-hourly-forecast-container {
    width: 100%;
    height: 300px;

    .place-hourly-forecast-container__chart {
      padding: 4px;
      width: 300px;
      height: 200px;
    }
  }
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""