summaryrefslogtreecommitdiff
path: root/frontend/src/components/app/sidebars/elements/LoadChartComponent.js
blob: ae52891a2ad8ce742d85d402d75d72bfcc38171b (plain)
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
import React from 'react'
import ReactDOM from 'react-dom/server'
import SvgSaver from 'svgsaver'
import { VictoryAxis, VictoryChart, VictoryLabel, VictoryLine, VictoryScatter } from 'victory'
import { convertSecondsToFormattedTime } from '../../../../util/date-time'

const LoadChartComponent = ({ data, currentTick }) => {
    const onExport = () => {
        const div = document.createElement('div')
        div.innerHTML = ReactDOM.renderToString(
            <VictoryChartComponent
                data={data}
                currentTick={currentTick}
                showCurrentTick={false}
            />,
        )
        div.firstChild.style =
            'font-family: Roboto, Arial, sans-serif; font-size: 10pt;'
        const svgSaver = new SvgSaver()
        svgSaver.asSvg(
            div.firstChild,
            'opendc-chart-export-' + Date.now() + '.svg',
        )
    }

    return (
        <div className="mt-1" style={{ position: 'relative' }}>
            <strong>Load over time</strong>
            <VictoryChartComponent
                data={data}
                currentTick={currentTick}
                showCurrentTick={true}
            />
            <ExportChartComponent onExport={onExport}/>
        </div>
    )
}

const VictoryChartComponent = ({ data, currentTick, showCurrentTick }) => (
    <VictoryChart
        height={250}
        padding={{ top: 10, bottom: 50, left: 50, right: 50 }}
    >
        <VictoryAxis
            tickFormat={tick => convertSecondsToFormattedTime(tick)}
            fixLabelOverlap={true}
            label="Simulated Time"
        />
        <VictoryAxis dependentAxis label="Load"/>
        <VictoryLine data={data}/>
        <VictoryScatter data={data}/>
        {showCurrentTick ? (
            <VictoryLine
                labelComponent={
                    <VictoryLabel renderInPortal angle={90} dy={-5} dx={60}/>
                }
                data={[{ x: currentTick + 1, y: 0 }, { x: currentTick + 1, y: 1 }]}
                labels={point =>
                    point.y === 1
                        ? 'Current tick : ' + convertSecondsToFormattedTime(currentTick)
                        : ''}
                style={{
                    data: { stroke: '#00A6D6', strokeWidth: 4 },
                    labels: { fill: '#00A6D6' },
                }}
            />
        ) : (
            undefined
        )}
    </VictoryChart>
)

const ExportChartComponent = ({ onExport }) => (
    <button
        className="btn btn-success btn-circle btn-sm"
        title="Export Chart to PNG Image"
        onClick={onExport}
        style={{ position: 'absolute', top: 0, right: 0 }}
    >
        <span className="fa fa-camera"/>
    </button>
)

export default LoadChartComponent