}
impl Visualization {
+ fn decode_labels(&self, graph: &Graph) -> (Option<Value>, Option<Value>, pivot::Footnotes) {
+ let mut labels = EnumMap::from_fn(|_| Vec::new());
+ for child in &self.children {
+ match child {
+ VisChild::LabelFrame(label_frame) => {
+ if let Some(label) = &label_frame.label
+ && let Some(purpose) = label.purpose
+ {
+ labels[purpose].push(label);
+ }
+ }
+ VisChild::Container(c) => {
+ for label_frame in &c.label_frames {
+ if let Some(label) = &label_frame.label
+ && let Some(purpose) = label.purpose
+ {
+ labels[purpose].push(label);
+ }
+ }
+ }
+ _ => (),
+ }
+ }
+ let caption_labels = if !labels[Purpose::SubTitle].is_empty() {
+ &labels[Purpose::SubTitle]
+ } else {
+ &labels[Purpose::Footnote]
+ };
+
+ let footnotes = self.decode_footnotes(graph, &labels[Purpose::Footnote]);
+ (
+ LabelFrame::decode_label(&labels[Purpose::Title], &footnotes),
+ LabelFrame::decode_label(caption_labels, &footnotes),
+ footnotes,
+ )
+ }
fn decode_date(&self) -> Result<NaiveDate, LegacyXmlWarning> {
NaiveDate::parse_from_str(&self.date, "%Y-%m-%d")
.map_err(|_| LegacyXmlWarning::InvalidCreationDate(self.date.clone()))
}
}
+ pub fn graph(&self) -> Result<&Graph, super::Error> {
+ for child in &self.children {
+ match child {
+ VisChild::Graph(g) => return Ok(g),
+ _ => (),
+ }
+ }
+ Err(super::Error::LegacyMissingGraph)
+ }
+
pub fn decode(
&self,
binary_data: IndexMap<String, IndexMap<String, Vec<Datum<String>>>>,
look: Look,
warn: &mut dyn FnMut(LegacyXmlWarning),
) -> Result<PivotTable, super::Error> {
- let mut graph = None;
- let mut labels = EnumMap::from_fn(|_| Vec::new());
- for child in &self.children {
- match child {
- VisChild::Graph(g) => graph = Some(g),
- VisChild::LabelFrame(label_frame) => {
- if let Some(label) = &label_frame.label
- && let Some(purpose) = label.purpose
- {
- labels[purpose].push(label);
- }
- }
- VisChild::Container(c) => {
- for label_frame in &c.label_frames {
- if let Some(label) = &label_frame.label
- && let Some(purpose) = label.purpose
- {
- labels[purpose].push(label);
- }
- }
- }
- VisChild::Style(_)
- | VisChild::SourceVariable(_)
- | VisChild::DerivedVariable(_)
- | VisChild::Other => (),
- }
- }
- let Some(graph) = graph else {
- return Err(super::Error::LegacyMissingGraph);
- };
-
- let footnotes = self.decode_footnotes(graph, &labels[Purpose::Footnote]);
-
- let title = LabelFrame::decode_label(&labels[Purpose::Title], &footnotes);
- let caption_labels = if !labels[Purpose::SubTitle].is_empty() {
- &labels[Purpose::SubTitle]
- } else {
- &labels[Purpose::Footnote]
- };
- let caption = LabelFrame::decode_label(caption_labels, &footnotes);
+ let graph = self.graph()?;
+ let (title, caption, footnotes) = self.decode_labels(graph);
let series = self.decode_series(binary_data, warn);
let (mut dims, current_layer) = self.decode_dimensions(graph, &series, &footnotes);