Skip to content

Commit 5d4743e

Browse files
committed
Added support for animated line
1 parent ea4b3b5 commit 5d4743e

File tree

4 files changed

+236
-7
lines changed

4 files changed

+236
-7
lines changed

plotly/plotly_offline_aux/plotlyoffline.m

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,20 @@
3939
link_text = '';
4040
end
4141

42+
% check if an animation
43+
if ~isempty(plotlyfig.frames)
44+
isAnimation = true;
45+
else
46+
isAnimation = false;
47+
end
48+
4249
% format the data and layout
4350
jdata = m2json(plotlyfig.data);
4451
jlayout = m2json(plotlyfig.layout);
52+
jframes = m2json(plotlyfig.frames);
4553
clean_jdata = escapechars(jdata);
4654
clean_jlayout = escapechars(jlayout);
55+
clean_jframes = escapechars(jframes);
4756

4857
% template environment vars
4958
plotly_domain = plotlyfig.UserData.PlotlyDomain;
@@ -52,14 +61,22 @@
5261
'window.PLOTLYENV.BASE_URL="%s";', ...
5362
'Plotly.LINKTEXT="%s";', ...
5463
'</script>'], plotly_domain, link_text);
55-
56-
% template Plotly.plot
57-
script = sprintf(['\n Plotly.plot("%s", %s, %s).then(function(){'...
58-
'\n $(".%s.loading").remove();' ...
59-
'\n $(".link--embedview").text("%s");'...
60-
'\n });'], id, clean_jdata, clean_jlayout, ...
61-
id, link_text);
6264

65+
% template Plotly.plot
66+
if isAnimation
67+
script = sprintf(['\n Plotly.plot("%s", {\n"data": %s,\n"layout": %s,\n"frames": %s\n}).then(function(){'...
68+
'\n $(".%s.loading").remove();' ...
69+
'\n $(".link--embedview").text("%s");'...
70+
'\n });'], id, clean_jdata, clean_jlayout, clean_jframes,...
71+
id, link_text);
72+
else
73+
script = sprintf(['\n Plotly.plot("%s", %s, %s).then(function(){'...
74+
'\n $(".%s.loading").remove();' ...
75+
'\n $(".link--embedview").text("%s");'...
76+
'\n });'], id, clean_jdata, clean_jlayout, ...
77+
id, link_text);
78+
end
79+
6380
plotly_script = sprintf(['\n<div id="%s" style="height: %s;',...
6481
'width: %s;" class="plotly-graph-div">' ...
6582
'</div> \n<script type="text/javascript">' ...

plotly/plotlyfig.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
properties
55
data; % data of the plot
66
layout; % layout of the plot
7+
frames; % for animations
78
url; % url response of making post request
89
error; % error response of making post request
910
warning; % warning response of making post request
@@ -35,6 +36,7 @@
3536
%-Core-%
3637
obj.data = {};
3738
obj.layout = struct();
39+
obj.frames = {};
3840
obj.url = '';
3941

4042
obj.UserData.Verbose = true;

plotly/plotlyfig_aux/core/updateData.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
updateArea(obj, dataIndex);
8080
case 'areaseries'
8181
updateAreaseries(obj, dataIndex);
82+
case 'animatedline'
83+
updateAnimatedLine(obj, dataIndex);
8284
case 'bar'
8385
updateBar(obj, dataIndex);
8486
case 'barseries'
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
function updateAnimatedLine(obj,plotIndex)
2+
3+
%----SCATTER FIELDS----%
4+
5+
% x - [DONE]
6+
% y - [DONE]
7+
% r - [HANDLED BY SCATTER]
8+
% t - [HANDLED BY SCATTER]
9+
% mode - [DONE]
10+
% name - [NOT SUPPORTED IN MATLAB]
11+
% text - [DONE]
12+
% error_y - [HANDLED BY ERRORBAR]
13+
% error_x - [NOT SUPPORTED IN MATLAB]
14+
% connectgaps - [NOT SUPPORTED IN MATLAB]
15+
% fill - [HANDLED BY AREA]
16+
% fillcolor - [HANDLED BY AREA]
17+
% opacity --- [TODO]
18+
% textfont - [NOT SUPPORTED IN MATLAB]
19+
% textposition - [NOT SUPPORTED IN MATLAB]
20+
% xaxis [DONE]
21+
% yaxis [DONE]
22+
% showlegend [DONE]
23+
% stream - [HANDLED BY PLOTLYSTREAM]
24+
% visible [DONE]
25+
% type [DONE]
26+
27+
% MARKER
28+
% marler.color - [DONE]
29+
% marker.size - [DONE]
30+
% marker.line.color - [DONE]
31+
% marker.line.width - [DONE]
32+
% marker.line.dash - [NOT SUPPORTED IN MATLAB]
33+
% marker.line.opacity - [NOT SUPPORTED IN MATLAB]
34+
% marker.line.smoothing - [NOT SUPPORTED IN MATLAB]
35+
% marker.line.shape - [NOT SUPPORTED IN MATLAB]
36+
% marker.opacity --- [TODO]
37+
% marker.colorscale - [NOT SUPPORTED IN MATLAB]
38+
% marker.sizemode - [NOT SUPPORTED IN MATLAB]
39+
% marker.sizeref - [NOT SUPPORTED IN MATLAB]
40+
% marker.maxdisplayed - [NOT SUPPORTED IN MATLAB]
41+
42+
% LINE
43+
44+
% line.color - [DONE]
45+
% line.width - [DONE]
46+
% line.dash - [DONE]
47+
% line.opacity --- [TODO]
48+
% line.smoothing - [NOT SUPPORTED IN MATLAB]
49+
% line.shape - [NOT SUPPORTED IN MATLAB]
50+
51+
%-------------------------------------------------------------------------%
52+
53+
%-AXIS INDEX-%
54+
axIndex = obj.getAxisIndex(obj.State.Plot(plotIndex).AssociatedAxis);
55+
56+
animLine = obj.State.Plot(plotIndex).AssociatedAxis.Children;
57+
58+
%-PLOT DATA STRUCTURE- %
59+
plot_data = get(obj.State.Plot(plotIndex).Handle);
60+
61+
%-CHECK FOR MULTIPLE AXES-%
62+
[xsource, ysource] = findSourceAxis(obj,axIndex);
63+
64+
%-AXIS DATA-%
65+
eval(['xaxis = obj.layout.xaxis' num2str(xsource) ';']);
66+
eval(['yaxis = obj.layout.yaxis' num2str(ysource) ';']);
67+
68+
%-------------------------------------------------------------------------%
69+
70+
%-if polar plot or not-%
71+
treatas = obj.PlotOptions.TreatAs;
72+
ispolar = strcmpi(treatas, 'compass') || strcmpi(treatas, 'ezpolar');
73+
74+
%-------------------------------------------------------------------------%
75+
76+
%-getting data-%
77+
[x,y] = getpoints(animLine);
78+
79+
%-------------------------------------------------------------------------%
80+
81+
%-scatter xaxis-%
82+
obj.data{plotIndex}.xaxis = ['x' num2str(xsource)];
83+
84+
%-------------------------------------------------------------------------%
85+
86+
%-scatter yaxis-%
87+
obj.data{plotIndex}.yaxis = ['y' num2str(ysource)];
88+
89+
%-------------------------------------------------------------------------%
90+
91+
%-scatter type-%
92+
obj.data{plotIndex}.type = 'scatter';
93+
94+
if ispolar
95+
obj.data{plotIndex}.type = 'scatterpolar';
96+
end
97+
98+
%-------------------------------------------------------------------------%
99+
100+
%-scatter visible-%
101+
obj.data{plotIndex}.visible = strcmp(plot_data.Visible,'on');
102+
103+
%-------------------------------------------------------------------------%
104+
105+
%-scatter x-%
106+
107+
if ispolar
108+
r = sqrt(x.^2 + y.^2);
109+
obj.data{plotIndex}.r = r;
110+
else
111+
obj.data{plotIndex}.x = x(1);
112+
end
113+
114+
%-------------------------------------------------------------------------%
115+
116+
%-scatter y-%
117+
if ispolar
118+
theta = atan2(x,y);
119+
obj.data{plotIndex}.theta = -(rad2deg(theta) - 90);
120+
else
121+
obj.data{plotIndex}.y = y(1);
122+
end
123+
124+
%-------------------------------------------------------------------------%
125+
126+
%-Fro 3D plots-%
127+
obj.PlotOptions.is3d = false; % by default
128+
129+
if isfield(plot_data,'ZData')
130+
131+
numbset = unique(plot_data.ZData);
132+
133+
if any(plot_data.ZData) && length(numbset)>1
134+
%-scatter z-%
135+
obj.data{plotIndex}.z = plot_data.ZData;
136+
137+
%-overwrite type-%
138+
obj.data{plotIndex}.type = 'scatter3d';
139+
140+
%-flag to manage 3d plots-%
141+
obj.PlotOptions.is3d = true;
142+
end
143+
end
144+
145+
%-------------------------------------------------------------------------%
146+
147+
%-scatter name-%
148+
obj.data{plotIndex}.name = plot_data.DisplayName;
149+
150+
%-------------------------------------------------------------------------%
151+
152+
%-scatter mode-%
153+
if ~strcmpi('none', plot_data.Marker) ...
154+
&& ~strcmpi('none', plot_data.LineStyle)
155+
mode = 'lines+markers';
156+
elseif ~strcmpi('none', plot_data.Marker)
157+
mode = 'markers';
158+
elseif ~strcmpi('none', plot_data.LineStyle)
159+
mode = 'lines';
160+
else
161+
mode = 'none';
162+
end
163+
164+
obj.data{plotIndex}.mode = mode;
165+
166+
%-------------------------------------------------------------------------%
167+
168+
%-scatter line-%
169+
obj.data{plotIndex}.line = extractLineLine(plot_data);
170+
171+
%-------------------------------------------------------------------------%
172+
173+
%-scatter marker-%
174+
obj.data{plotIndex}.marker = extractLineMarker(plot_data);
175+
176+
%-------------------------------------------------------------------------%
177+
178+
%-scatter showlegend-%
179+
leg = get(plot_data.Annotation);
180+
legInfo = get(leg.LegendInformation);
181+
182+
switch legInfo.IconDisplayStyle
183+
case 'on'
184+
showleg = true;
185+
case 'off'
186+
showleg = false;
187+
end
188+
189+
obj.data{plotIndex}.showlegend = showleg;
190+
191+
%-------------------------------------------------------------------------%
192+
% Play Button
193+
args = {NaN,struct('frame',struct('duration',0,'redraw',false),'mode','immediate','transition',struct('duration',0))};
194+
c{1} = struct('label','&#9654;','method','animate','args',{args});
195+
196+
% Stop/Pause Button
197+
% c{2} = struct('label','&#9724;','method','animate','args',{args2});
198+
199+
obj.layout.updatemenus={struct('buttons',{c},'type','buttons','pad',struct('r',70,'t',10),...
200+
'direction','left','showactive',true,'x',0.01,'xanchor','left','y',0.01,'yanchor','middle')};
201+
DD{plotIndex} = obj.data{plotIndex};
202+
203+
for i = 1:1:length(x)
204+
DD{plotIndex}.x=x(1:i);
205+
DD{plotIndex}.y=y(1:i);
206+
obj.frames{end+1} = struct('name',['f',num2str(i)],'data',{DD});
207+
end
208+
end

0 commit comments

Comments
 (0)