Dynamically set color of line/spline in Nativescript-UI-Chart?

544 Views Asked by At

I need to match the color of each line in a chart to the color given by the object providing the data. The meter.color variable holds the color I need to use.

<StackLayout class="container" orientation="vertical">
    <Label class="title" [text]="widget.settings.title" horizontalAlignment="center"></Label>
    <RadCartesianChart class="chart" tkExampleTitle tkToggleNavButton>
        <ng-container *ngFor="let meter of readings$ | async">
            <ng-container [ngSwitch]="meter.type">

                <LineSeries tkCartesianSeries seriesName="Line" *ngSwitchCase="'line'" [legendTitle]="meter.name + ' - ' + meter.unit"
                    [items]="meter.data" stackMode="Stack" categoryProperty="timestamp" valueProperty="value">
                    <Palette tkCartesianPalette seriesName="Line">
                        <PaletteEntry tkCartesianPaletteEntry [strokeColor]="meter.color"></PaletteEntry>
                    </Palette>
                </LineSeries>

                <SplineSeries tkCartesianSeries seriesName="Spline" *ngSwitchCase="'spline'" [legendTitle]="meter.name"
                    [items]="meter.data" stackMode="Stack" categoryProperty="timestamp" valueProperty="value">
                    <Palette tkCartesianPalette seriesName="Spline">
                        <PaletteEntry tkCartesianPaletteEntry [strokeColor]="meter.color"></PaletteEntry>
                    </Palette>
                </SplineSeries>
                
            </ng-container>
            <RadLegendView tkCartesianLegend position="Top" title="Series" enableSelection="true"></RadLegendView>
            <RadCartesianChartGrid tkCartesianGrid horizontalLinesVisible="true" verticalStrokeColor="#804d0026"></RadCartesianChartGrid>
            <LinearAxis tkCartesianVerticalAxis horizontalLocation="Left" android:labelFormat="%.0f"></LinearAxis>
            <DateTimeContinuousAxis tkCartesianHorizontalAxis dateFormat="hh:mm" [minimum]="backwardHour" [maximum]="forwardHour"
                majorStep="Hour" labelFitMode="Rotate"></DateTimeContinuousAxis>
        </ng-container>
    </RadCartesianChart>
</StackLayout>

At the moment the line gets one color and the spline get another. But if i have more than two lines or splines all lines get the same color and all splines also get the same color. It seems that the last line added sets the color of both lines, the same goes for the spline.

1

There are 1 best solutions below

0
On

I found a solution to my problem, i added property binding to seriesName on both the LineSeries and to the PaletteEntry witch matches each line dynamically to a ID.

Changes in my code:

<LineSeries tkCartesianSeries [seriesName]="meter.id" *ngSwitchCase="'line'" [legendTitle]="meter.name + ' - ' + register.unit" [items]="register.data" stackMode="Stack" categoryProperty="timestamp" valueProperty="value">
  <Palette tkCartesianPalette [seriesName]="meter.id">
    <PaletteEntry tkCartesianPaletteEntry [strokeColor]="meter.color"></PaletteEntry>
  </Palette>
</LineSeries>