Bonjour à tous,
Après un titre barbar, je vous dois quelques explications.
Le but étant de faire un module Drupal avec la librairie www.highcharts.com/demo/
J'ai toute la mécanique sauf la génération du JS de config qui me pose problème.
je dois obtenir le JS suivant (si c'est plus simple pour vous à comprendre ce sera une chaîne de caractère dans mon module):
Depuis le XML suivant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 var chart; jQuery(document).ready(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'container', defaultSeriesType: 'column' }, title: { text: 'Monthly Average Rainfall' }, subtitle: { text: 'Source: WorldClimate.com' }, xAxis: { categories: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] }, yAxis: { min: 0, title: { text: 'Rainfall (mm)' } }, legend: { layout: 'vertical', backgroundColor: '#FFFFFF', align: 'left', verticalAlign: 'top', x: 100, y: 70, floating: true, shadow: true }, series: [{ name: 'Tokyo', data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] }, { name: 'New York', data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3] }, { name: 'London', data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2] }, { name: 'Berlin', data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1] }] }); });
La structure XML est de moi, elle peut être modifiée.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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<?xml version="1.0" encoding="UTF-8"?> <root> <chart> <renderTo>container</renderTo> <defaultSeriesType>column</defaultSeriesType> </chart> <title> <text><![CDATA[Monthly Average Rainfall]]></text> </title> <subtitle> <text><![CDATA[Monthly Average Rainfall]]></text> </subtitle> <xAxis> <categories> <categorie>Jan</categorie> <categorie>Feb</categorie> <categorie>Mar</categorie> <categorie>Apr</categorie> <categorie>May</categorie> <categorie>Jun</categorie> <categorie>Jul</categorie> <categorie>Aug</categorie> <categorie>Sep</categorie> <categorie>Oct</categorie> <categorie>Nov</categorie> <categorie>Dec</categorie> </categories> <title></title> </xAxis> <yAxis> <min>0</min> <max></max> <title><![CDATA[Rainfall (mm)]]></title> </yAxis> <legend> <layout>vertical</layout> <align>left</align> <verticalAlign>top</verticalAlign> <x>100</x> <y>70</y> <floating>true</floating> <shadow>true</shadow> </legend> <series> <serie> <name>Tokyo</name> <datas> <data>49.9</data> <data>71.5</data> <data>106.4</data> <data>129.2</data> <data>144.0</data> <data>176.0</data> <data>135.6</data> <data>148.5</data> <data>216.4</data> <data>194.1</data> <data>95.6</data> <data>54.4</data> </datas> </serie> <serie> <name>New York</name> <datas> <data>83.6</data> <data>78.8</data> <data>98.5</data> <data>93.4</data> <data>106.0</data> <data>84.5</data> <data>105.0</data> <data>104.3</data> <data>91.2</data> <data>83.5</data> <data>106.6</data> <data>92.3</data> </datas> </serie> <serie> <name>London</name> <datas> <data>48.9</data> <data>38.8</data> <data>39.3</data> <data>41.4</data> <data>47.0</data> <data>48.3</data> <data>59.0</data> <data>59.6</data> <data>52.4</data> <data>65.2</data> <data>59.3</data> <data>51.2</data> </datas> </serie> <serie> <name>Berlin</name> <datas> <data>42.4</data> <data>33.2</data> <data>34.5</data> <data>39.7</data> <data>52.6</data> <data>75.5</data> <data>57.4</data> <data>60.4</data> <data>47.6</data> <data>39.1</data> <data>46.8</data> <data>51.1</data> </datas> </serie> </series> </root>
Je me suis dis qu'un algo récursif serait le plus approprié mais comme il y a plusieurs façon d'écrire les arguments.
Ma première question, selon vous, quelle est la librairie la plus adaptée pour parser le XML j'ai utilisé DOM (http://eusebius.developpez.com/php5dom/)
Ensuite, je suis parti sur un parcours avec des foreach sur mon objet mais je me rend compte que ce n'est pas viable car j'ai deja 3 boucles imbriqués et je n'ai pas traité toutes les possibilités.
Alors je voudrais repartir sur de bonne bases avec un algo propre d'où ce post
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 $xml_obj = fileToObject($xml_filename); $custom = ""; foreach ($xml_obj->root->children as $niv1) { //niv 1 $custom .= $niv1->name . ":{\n\t"; if (isset($niv1->children)) { $items = array(); foreach ($niv1->children as $niv2) { if (isset($niv2->children)) { $niv2_output = ''; $niv2_elemts = array(); $niv2_output .= $niv2->name .": ["; foreach($niv2->children as $niv3){ if($niv3->textValue) $niv2_elemts[] = "'". $niv3->textValue . "'"; } $niv2_output .= implode(",",$niv2_elemts); $items[] .= $niv2_output . "]"; } else { $items[] = $niv2->name . ": '" . $niv2->textValue ."'"; } } $custom .= implode(",\n\t", $items); } $custom .= "\n}, \n\n"; }
Si vous avez des questions, n'hésitez pas.
Par avance merci
Partager