Allow custom options for charts

This commit is contained in:
Thomas Nordquist
2019-06-17 16:37:13 +02:00
parent 2296143883
commit 0f9c2cd36f
10 changed files with 367 additions and 39 deletions

View File

@@ -2,9 +2,16 @@ import { Action } from 'redux'
import { createReducer } from './lib'
import { Record, List } from 'immutable'
export type PlotCurveTypes = 'curve' | 'linear' | 'cubic_basis_spline' | 'step_after' | 'step_before'
export interface ChartParameters {
topic: string
dotPath?: string
interpolation?: PlotCurveTypes
range?: {
from?: number
to?: number
}
}
export interface ChartsStateModel {
@@ -13,12 +20,13 @@ export interface ChartsStateModel {
export type ChartsState = Record<ChartsStateModel>
export type Action = AddChart | RemoveChart | SetCharts
export type Action = AddChart | RemoveChart | SetCharts | UpdateChart
export enum ActionTypes {
CHARTS_ADD = 'CHARTS_ADD',
CHARTS_REMOVE = 'CHARTS_REMOVE',
CHARTS_SET = 'CHARTS_SET',
CHARTS_UPDATE = 'CHARTS_UPDATE',
}
export interface AddChart {
@@ -26,6 +34,13 @@ export interface AddChart {
chart: ChartParameters
}
export interface UpdateChart {
type: ActionTypes.CHARTS_ADD
topic: string
dotPath?: string
parameters: Partial<ChartParameters>
}
export interface RemoveChart {
type: ActionTypes.CHARTS_REMOVE
chart: ChartParameters
@@ -44,12 +59,21 @@ export const chartsReducer = createReducer(initialState(), {
CHARTS_ADD: addChart,
CHARTS_REMOVE: removeChart,
CHARTS_SET: setCharts,
CHARTS_UPDATE: updateChart,
})
function addChart(state: ChartsState, action: AddChart) {
return state.set('charts', state.get('charts').push(action.chart))
}
function updateChart(state: ChartsState, action: UpdateChart) {
const charts = state.get('charts')
const chartIdx = charts.findIndex(chart => chart.topic === action.topic && chart.dotPath === action.dotPath)
const chart = charts.get(chartIdx)
return state.set('charts', chart ? charts.set(chartIdx, { ...chart, ...action.parameters }) : charts)
}
function removeChart(state: ChartsState, action: RemoveChart) {
const charts = state.get('charts')
const newCharts = charts.filter(chart => chart.topic !== action.chart.topic || chart.dotPath !== action.chart.dotPath)