UNPKG

@nativescript-community/ui-chart

Version:

A powerful chart / graph plugin, supporting line, bar, pie, radar, bubble, and candlestick charts as well as scaling, panning and animations.

225 lines 6.45 kB
import { LegendForm } from '../components/Legend'; import { AxisDependency } from '../components/YAxis'; import { Color } from '@nativescript/core/color'; import { Utils } from '../utils/Utils'; /** * Created by Philipp Jahoda on 21/10/15. * This is the base dataset of all DataSets. It's purpose is to implement critical methods * provided by the IDataSet interface. */ export class BaseDataSet { /** * Constructor with label. * * @param label */ constructor(label, xProperty, yProperty) { /** * property to access the "x" value of an entry for this set * */ this.xProperty = 'x'; /** * property to access the "y" value of an entry for this set * */ this.yProperty = 'y'; /** * property to access the "icon" value of an entry for this set * */ this.iconProperty = 'icon'; /** * List representing all colors that are used for this DataSet */ this.colors = null; this.color = '#8CEAFF'; /** * List representing all colors that are used for drawing the actual values for this DataSet */ this.valueColors = ['black']; /** * label that describes the DataSet or the data the DataSet represents */ this.label = 'DataSet'; /** * this specifies which axis this DataSet should be plotted against */ this.axisDependency = AxisDependency.LEFT; /** * if true, value highlightning is enabled */ this.highlightEnabled = true; /** * custom formatter that is used instead of the auto-formatter if set */ this.valueFormatter = Utils.getDefaultValueFormatter(); this.form = LegendForm.DEFAULT; this.formSize = NaN; this.formLineWidth = NaN; this.formLineDashEffect = null; /** * if true, y-values are drawn on the chart */ this.drawValuesEnabled = false; /** * the offset for drawing values (in dp) */ this.valuesOffset = { x: 0, y: 0 }; /** * if true, y-icons are drawn on the chart */ this.drawIconsEnabled = false; /** * the offset for drawing icons (in dp) */ this.iconsOffset = { x: 0, y: 0 }; /** * the size of the value-text labels */ this.valueTextSize = 13; /** * flag that indicates if the DataSet is visible or not */ this.visible = true; /** * data space from the smallest value to the bottom in percent of the total axis range */ this.spaceBottom = 0; /** * data space from the highest value to the top in percent of the total axis range */ this.spaceTop = 0; // if (xProperty) { this.xProperty = xProperty; // } if (yProperty) { this.yProperty = yProperty; } if (!this.xProperty) { this.getEntryXValue = function (e, entryIndex) { return entryIndex; }; } else { this.getEntryXValue = (e, entryIndex) => e[this.xProperty]; } this.label = label; } getEntryXValue(e, entryIndex) { return this.xProperty ? e[this.xProperty] : entryIndex; } getEntryIcon(e) { return e[this.iconProperty]; } /** * Use this method to tell the data set that the underlying data has changed. */ notifyDataSetChanged() { this.calcMinMax(); } /** * ###### ###### COLOR GETTING RELATED METHODS ##### ###### */ getColor(index = 0) { if (this.colors?.length > 0) { return this.colors[Math.floor(index) % this.colors.length]; } return this.color; } /** * Adds a new color to the colors array of the DataSet. * * @param color */ addColor(value) { if (!this.colors) this.colors = []; this.colors.push(value); } /** * Sets a color with a specific alpha value. * * @param color * @param alpha from 0-255 */ setColor(color, alpha) { if (alpha !== undefined) { const actColor = color instanceof Color ? color : new Color(color); color = new Color(actColor.r, actColor.g, actColor.b, alpha); } this.color = color; } /** * Sets colors with a specific alpha value. * * @param colors * @param alpha */ setColors(colors, alpha) { // this.resetColors(); if (alpha === undefined) { this.colors = colors; } else { this.colors = colors.map((c) => new Color(c.r, c.g, c.b, alpha)); } } /** * Resets all colors of this DataSet and recreates the colors array. */ resetColors() { this.colors = []; } /** * ###### ###### OTHER STYLING RELATED METHODS ##### ###### */ get needsFormatter() { return !this.valueFormatter; } set valueTextColor(color) { this.valueColors = [color]; } set valueTextColors(colors) { this.valueColors = colors; } getValueTextColor(index = 0) { return this.valueColors[Math.floor(index) % this.valueColors.length]; } /** * ###### ###### DATA RELATED METHODS ###### ###### */ getIndexInEntries(xIndex) { for (let i = 0; i < this.entryCount; i++) { if (xIndex === this.getEntryForIndex(i).x) return i; } return -1; } removeFirst() { if (this.entryCount > 0) { return this.removeEntryAtIndex(0); } else return false; } removeLast() { if (this.entryCount > 0) { return this.removeEntryAtIndex(this.entryCount - 1); } else return false; } removeEntryByXValue(xValue) { const e = this.getEntryForXValue(xValue, NaN); return this.removeEntry(e); } contains(e) { for (let i = 0; i < this.entryCount; i++) { if (e === this.getEntryForIndex(i)) return true; } return false; } } //# sourceMappingURL=BaseDataSet.js.map