plotly 0.14.1

A plotting library powered by Plotly.js
Documentation
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Animation support for Plotly.rs
//!
//! This module provides animation configuration for Plotly.js updatemenu
//! buttons and slider steps, following the Plotly.js animation API
//! specification.

use plotly_derive::FieldSetter;
use serde::ser::{SerializeSeq, Serializer};
use serde::Serialize;

use crate::{Layout, Traces};

/// A frame represents a single state in an animation sequence.
/// Based on Plotly.js frame_attributes.js specification
#[serde_with::skip_serializing_none]
#[derive(Serialize, Clone, FieldSetter)]
pub struct Frame {
    /// An identifier that specifies the group to which the frame belongs,
    /// used by animate to select a subset of frames
    group: Option<String>,
    /// A label by which to identify the frame
    name: Option<String>,
    /// A list of trace indices that identify the respective traces in the data
    /// attribute
    traces: Option<Vec<usize>>,
    /// The name of the frame into which this frame's properties are merged
    /// before applying. This is used to unify properties and avoid needing
    /// to specify the same values for the same properties in multiple
    /// frames.
    baseframe: Option<String>,
    /// A list of traces this frame modifies. The format is identical to the
    /// normal trace definition.
    data: Option<Traces>,
    /// Layout properties which this frame modifies. The format is identical to
    /// the normal layout definition.
    layout: Option<Layout>,
}

impl Frame {
    pub fn new() -> Self {
        Default::default()
    }
}

/// Represents the animation arguments array for Plotly.js
/// Format: [frameNamesOrNull, animationOptions]
#[derive(Clone, Debug)]
pub struct Animation {
    /// Frames sequence: null, [null], or array of frame names
    frames: FrameListMode,
    /// Animation options/configuration
    options: AnimationOptions,
}

impl Default for Animation {
    fn default() -> Self {
        Self {
            frames: FrameListMode::All,
            options: AnimationOptions::default(),
        }
    }
}

impl Animation {
    /// Create a new animation args with default values for options and
    /// FramesMode set to all frames
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a animation for playing all frames (default)    
    pub fn all_frames() -> Self {
        Self::new()
    }

    /// Create a animation setup specifically for pausing a running animation
    pub fn pause() -> Self {
        Self {
            frames: FrameListMode::Pause,
            options: AnimationOptions::new()
                .mode(AnimationMode::Immediate)
                .frame(FrameSettings::new().duration(0).redraw(false))
                .transition(TransitionSettings::new().duration(0)),
        }
    }

    /// Create animation args for specific frames
    pub fn frames(frames: Vec<String>) -> Self {
        Self {
            frames: FrameListMode::Frames(frames),
            ..Default::default()
        }
    }

    /// Set the animation options
    pub fn options(mut self, options: AnimationOptions) -> Self {
        self.options = options;
        self
    }
}

impl Serialize for Animation {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut seq = serializer.serialize_seq(Some(2))?;
        seq.serialize_element(&self.frames)?;
        seq.serialize_element(&self.options)?;
        seq.end()
    }
}

/// First argument in animation args - can be null, [null], or frame names
#[derive(Clone, Debug)]
pub enum FrameListMode {
    /// null - animate all frames
    All,
    /// Array of frame names to animate
    Frames(Vec<String>),
    /// special mode, [null], for pausing an animation
    Pause,
}

impl Serialize for FrameListMode {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            FrameListMode::All => serializer.serialize_unit(),
            FrameListMode::Pause => {
                let arr = vec![serde_json::Value::Null];
                arr.serialize(serializer)
            }
            FrameListMode::Frames(frames) => frames.serialize(serializer),
        }
    }
}

/// Animation configuration options
/// Based on actual Plotly.js animation API from animation_attributes.js
#[serde_with::skip_serializing_none]
#[derive(Serialize, Clone, Debug, FieldSetter)]
pub struct AnimationOptions {
    /// Frame animation settings
    frame: Option<FrameSettings>,
    /// Transition animation settings
    transition: Option<TransitionSettings>,
    /// Animation mode
    mode: Option<AnimationMode>,
    /// Animation direction
    direction: Option<AnimationDirection>,
    /// Play frames starting at the current frame instead of the beginning
    fromcurrent: Option<bool>,
}

impl AnimationOptions {
    pub fn new() -> Self {
        Default::default()
    }
}

/// Frame animation settings
#[serde_with::skip_serializing_none]
#[derive(Serialize, Clone, Debug, FieldSetter)]
pub struct FrameSettings {
    /// The duration in milliseconds of each frame
    duration: Option<usize>,
    /// Redraw the plot at completion of the transition
    redraw: Option<bool>,
}

impl FrameSettings {
    pub fn new() -> Self {
        Default::default()
    }
}

/// Transition animation settings
#[serde_with::skip_serializing_none]
#[derive(Serialize, Clone, Debug, FieldSetter)]
pub struct TransitionSettings {
    /// The duration of the transition, in milliseconds
    duration: Option<usize>,
    /// The easing function used for the transition
    easing: Option<AnimationEasing>,
    /// Determines whether the figure's layout or traces smoothly transitions
    ordering: Option<TransitionOrdering>,
}

impl TransitionSettings {
    pub fn new() -> Self {
        Default::default()
    }
}

/// Animation modes
#[derive(Serialize, Debug, Clone, Copy, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum AnimationMode {
    Immediate,
    Next,
    AfterAll,
}

/// Animation directions
#[derive(Serialize, Debug, Clone, Copy, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum AnimationDirection {
    Forward,
    Reverse,
}

/// Transition ordering options
#[derive(Serialize, Debug, Clone, Copy, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum TransitionOrdering {
    #[serde(rename = "layout first")]
    LayoutFirst,
    #[serde(rename = "traces first")]
    TracesFirst,
}

/// Easing functions for animation transitions
#[derive(Serialize, Debug, Clone, Copy, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum AnimationEasing {
    Linear,
    Quad,
    Cubic,
    Sin,
    Exp,
    Circle,
    Elastic,
    Back,
    Bounce,
    #[serde(rename = "linear-in")]
    LinearIn,
    #[serde(rename = "quad-in")]
    QuadIn,
    #[serde(rename = "cubic-in")]
    CubicIn,
    #[serde(rename = "sin-in")]
    SinIn,
    #[serde(rename = "exp-in")]
    ExpIn,
    #[serde(rename = "circle-in")]
    CircleIn,
    #[serde(rename = "elastic-in")]
    ElasticIn,
    #[serde(rename = "back-in")]
    BackIn,
    #[serde(rename = "bounce-in")]
    BounceIn,
    #[serde(rename = "linear-out")]
    LinearOut,
    #[serde(rename = "quad-out")]
    QuadOut,
    #[serde(rename = "cubic-out")]
    CubicOut,
    #[serde(rename = "sin-out")]
    SinOut,
    #[serde(rename = "exp-out")]
    ExpOut,
    #[serde(rename = "circle-out")]
    CircleOut,
    #[serde(rename = "elastic-out")]
    ElasticOut,
    #[serde(rename = "back-out")]
    BackOut,
    #[serde(rename = "bounce-out")]
    BounceOut,
    #[serde(rename = "linear-in-out")]
    LinearInOut,
    #[serde(rename = "quad-in-out")]
    QuadInOut,
    #[serde(rename = "cubic-in-out")]
    CubicInOut,
    #[serde(rename = "sin-in-out")]
    SinInOut,
    #[serde(rename = "exp-in-out")]
    ExpInOut,
    #[serde(rename = "circle-in-out")]
    CircleInOut,
    #[serde(rename = "elastic-in-out")]
    ElasticInOut,
    #[serde(rename = "back-in-out")]
    BackInOut,
    #[serde(rename = "bounce-in-out")]
    BounceInOut,
}

#[cfg(test)]
mod tests {
    use serde_json::{json, to_value};

    use super::*;
    use crate::Scatter;

    #[test]
    fn serialize_animation_easing() {
        let test_cases = [
            (AnimationEasing::Linear, "linear"),
            (AnimationEasing::Cubic, "cubic"),
            (AnimationEasing::CubicInOut, "cubic-in-out"),
            (AnimationEasing::ElasticInOut, "elastic-in-out"),
        ];

        for (easing, expected) in test_cases {
            assert_eq!(
                to_value(easing).unwrap(),
                json!(expected),
                "Failed for {:?}",
                easing
            );
        }
    }

    #[test]
    fn serialize_animation_mode() {
        let test_cases = [
            (AnimationMode::Immediate, "immediate"),
            (AnimationMode::Next, "next"),
            (AnimationMode::AfterAll, "afterall"),
        ];

        for (mode, expected) in test_cases {
            assert_eq!(
                to_value(mode).unwrap(),
                json!(expected),
                "Failed for {:?}",
                mode
            );
        }
    }

    #[test]
    fn serialize_animation_direction() {
        let test_cases = [
            (AnimationDirection::Forward, "forward"),
            (AnimationDirection::Reverse, "reverse"),
        ];

        for (direction, expected) in test_cases {
            assert_eq!(
                to_value(direction).unwrap(),
                json!(expected),
                "Failed for {:?}",
                direction
            );
        }
    }

    #[test]
    fn serialize_transition_ordering() {
        let test_cases = [
            (TransitionOrdering::LayoutFirst, "layout first"),
            (TransitionOrdering::TracesFirst, "traces first"),
        ];

        for (ordering, expected) in test_cases {
            assert_eq!(
                to_value(ordering).unwrap(),
                json!(expected),
                "Failed for {:?}",
                ordering
            );
        }
    }

    #[test]
    fn serialize_frame() {
        let frame = Frame::new()
            .name("test_frame")
            .group("test_group")
            .baseframe("base_frame");

        let expected = json!({
            "name": "test_frame",
            "group": "test_group",
            "baseframe": "base_frame"
        });

        assert_eq!(to_value(frame).unwrap(), expected);
    }

    #[test]
    fn serialize_frame_with_data() {
        let trace = Scatter::new(vec![1, 2, 3], vec![1, 2, 3]);
        let mut traces = Traces::new();
        traces.push(trace);

        let frame = Frame::new().name("frame_with_data").data(traces);

        let expected = json!({
            "name": "frame_with_data",
            "data": [
                {
                    "type": "scatter",
                    "x": [1, 2, 3],
                    "y": [1, 2, 3]
                }
            ]
        });

        assert_eq!(to_value(frame).unwrap(), expected);
    }

    #[test]
    fn serialize_animation() {
        let test_cases = [
            (
                Animation::all_frames(),
                json!(null),
                "all frames should serialize to null",
            ),
            (
                Animation::pause(),
                json!([null]),
                "pause should serialize to [null]",
            ),
            (
                Animation::frames(vec!["frame1".to_string(), "frame2".to_string()]),
                json!(["frame1", "frame2"]),
                "specific frames should serialize to frame names array",
            ),
        ];

        for (animation, expected_frames, description) in test_cases {
            let json = to_value(animation).unwrap();
            assert_eq!(json[0], expected_frames, "{}", description);
            assert!(json[1].is_object(), "Second element should be an object");
        }
    }

    #[test]
    fn serialize_animation_options_defaults() {
        let options = AnimationOptions::new();
        assert_eq!(to_value(options).unwrap(), json!({}));
    }

    #[test]
    fn serialize_animation_options() {
        let options = AnimationOptions::new()
            .mode(AnimationMode::Immediate)
            .direction(AnimationDirection::Forward)
            .fromcurrent(false)
            .frame(FrameSettings::new().duration(500).redraw(true))
            .transition(
                TransitionSettings::new()
                    .duration(300)
                    .easing(AnimationEasing::CubicInOut)
                    .ordering(TransitionOrdering::LayoutFirst),
            );

        let expected = json!({
            "mode": "immediate",
            "direction": "forward",
            "fromcurrent": false,
            "frame": {
                "duration": 500,
                "redraw": true
            },
            "transition": {
                "duration": 300,
                "easing": "cubic-in-out",
                "ordering": "layout first"
            }
        });

        assert_eq!(to_value(options).unwrap(), expected);
    }
}