-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsparklinesComponent.m
163 lines (136 loc) · 6.39 KB
/
sparklinesComponent.m
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
classdef (Sealed) sparklinesComponent < matlab.ui.componentcontainer.ComponentContainer
% sparklinesComponent Create small graphs which show the general trend of data.
%
% sparklinesComponent('Data',data) Create small graphs which show the
% general trend of the data in each row or column. By default the
% sparklines summarize each row and are stacked as a column.
%
% sparklinesComponent(___,Name,Value) specifies additional options for
% the sparklineComponent using one or more name-value pair arguments.
%
% sparklinesComponent(parent,___) create the sparkline component in the
% specified ui parent (e.g. uifigure, uigridlayout, etc.).
%
% h = sparklinesComponent(___) returns the sparklineComponent object.
% Use h to modify properties of the component after creating it.
%
% Copyright 2021 The MathWorks, Inc.
properties
Data (:,:) double = []
DataOrientation (1,1) string {mustBeMember(DataOrientation, {'row', 'column'})} = 'row';
SparklinesLayout (1,1) string {mustBeMember(SparklinesLayout, {'row', 'column'})} = 'column'
ColorStyle (1,1) string {mustBeMember(ColorStyle, {'gradient', 'solid'})} = 'solid'
SparklineColor (:,:) {validatecolor(SparklineColor)} = [0 0 0]
LimitColors (2,3) {mustBeNumeric} = [1 0 0; 0 0 1]
LineWidth (1,1) double {mustBeScalarOrEmpty} = 1;
SparklineStyle (1,1) string {mustBeMember(SparklineStyle, {'line', 'bar', 'dotted', 'dashed'})} = 'line'
end
properties(Access=private, Transient, NonCopyable)
% Height and width of each sparkline uihtml component
SparklineWidth double {mustBeScalarOrEmpty} = 210;
SparklineHeight double {mustBeScalarOrEmpty} = 50;
% uigridlayout which contains all sparkline uihtml components
LinesGrid matlab.ui.container.GridLayout
end
events (HasCallbackProperty, NotifyAccess=protected)
DataChanged
end
methods (Access=protected)
function setup(obj)
% Grid to contain sparklines
obj.LinesGrid = uigridlayout(obj, 'Padding', 0);
end
function update(obj)
% If data is empty, delete existing lines and return the empty grid
if isempty(obj.Data)
delete(obj.LinesGrid.Children)
return
end
% Display a warning if DataOrientation does not match with
% the standard SparklinesLayout
if strcmp(obj.DataOrientation, 'row') && ~strcmp(obj.SparklinesLayout, 'column')
warning(['Sparklines summarize data in each ' ...
'row of the Data property and are arranged in a row.'], 'dimensionsWarning');
elseif strcmp(obj.DataOrientation, 'column') && ~strcmp(obj.SparklinesLayout, 'row')
warning(['Sparklines summarize data in each ' ...
'column of the Data property and are arranged in a column.'], 'dimensionsWarning');
end
% Set background color of LinesGrid to be the same as the
% parent background color
obj.LinesGrid.BackgroundColor = obj.BackgroundColor;
obj.LinesGrid.Scrollable = 'on';
% If the color mode is 'gradient', determine the minimimum and
% maximum values of the matrix
minVal = min(obj.Data, [], 'all');
maxVal = max(obj.Data, [], 'all');
% Determine the number of sparklines to be drawn
if strcmp(obj.DataOrientation, 'row')
nLinesNeeded = size(obj.Data, 1);
elseif strcmp(obj.DataOrientation, 'column')
nLinesNeeded = size(obj.Data, 2);
end
% Determine the dimensions of the sparklines grid
if strcmp(obj.SparklinesLayout, 'column')
nLinesHave = numel(obj.LinesGrid.Children);
elseif strcmp(obj.SparklinesLayout, 'row')
nLinesHave = numel(obj.LinesGrid.Children);
end
% Create space for extra lines as needed
linesGrid = obj.LinesGrid;
% Create extra lines as needed
for n = nLinesHave+1:nLinesNeeded
sparkline = uihtml(linesGrid);
if strcmp(obj.SparklinesLayout, 'column')
sparkline.Layout.Row = n;
sparkline.Layout.Column = 1;
else
sparkline.Layout.Row = 1;
sparkline.Layout.Column = n;
end
sparkline.HTMLSource = fullfile(fileparts(mfilename('fullpath')), 'sparkline.html');
end
% Update data for sparklines
for i = 1:nLinesNeeded
sparkline = linesGrid.Children(i);
% Data to create the ith sparkline
if strcmp(obj.DataOrientation, 'row')
DataVector = obj.Data(i, :);
else
DataVector = obj.Data(:, i);
end
% Change data of htmlComponent
sparkline.Data = {obj.SparklineStyle, ...
obj.ColorStyle, ...
obj.LimitColors(1, :), obj.LimitColors(2, :), ...
minVal, maxVal, ...
obj.SparklineColor, ...
obj.LineWidth, ...
DataVector};
end
% Delete unneeded lines
delete(linesGrid.Children((nLinesNeeded + 1):numel(linesGrid.Children)));
obj.LinesGrid = linesGrid;
% Adjust sparklines grid height and width
if strcmp(obj.SparklinesLayout, 'column')
rowHeights = cell(1, nLinesNeeded);
rowHeights(:) = {obj.SparklineHeight};
obj.LinesGrid.RowHeight = rowHeights;
obj.LinesGrid.ColumnWidth = {obj.SparklineWidth};
elseif strcmp(obj.SparklinesLayout, 'row')
colWidths = cell(1, nLinesNeeded);
colWidths(:) = {obj.SparklineWidth};
obj.LinesGrid.ColumnWidth = colWidths;
obj.LinesGrid.RowHeight = {obj.SparklineHeight};
end
end
end
methods
function set.Data(obj, val)
obj.Data = val;
notify(obj, 'DataChanged')
end
function set.SparklineColor(obj, color)
obj.SparklineColor = validatecolor(color);
end
end
end