Skip to content

Commit 3686237

Browse files
Merge pull request #442 from contactashish13/issue-172-pro
Combo chart seems to be a regular line chart
2 parents a8cacce + 193d2c2 commit 3686237

File tree

1 file changed

+55
-7
lines changed

1 file changed

+55
-7
lines changed

classes/Visualizer/Module/Utility.php

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ private static function get_random_color() {
118118
}
119119

120120
/**
121-
* Sets some defaults (colors etc.) in the chart.
122-
* Currently only for ChartJS.
121+
* Sets some defaults in the chart.
123122
*
124123
* @since 3.3.0
125124
*
@@ -129,20 +128,69 @@ public static function set_defaults( $chart, $post_status = 'auto-draft' ) {
129128
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
130129
$library = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_LIBRARY, true );
131130

132-
if ( ( ! is_null( $post_status ) && $chart->post_status !== $post_status ) || $library !== 'ChartJS' ) {
131+
// if post_status is null, operate on the chart irrespective of the post_status
132+
if ( ( ! is_null( $post_status ) && $chart->post_status !== $post_status ) ) {
133133
return;
134134
}
135135

136-
$series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
137-
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
136+
$series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
138137
if ( ! $series || ! is_array( $series ) ) {
139138
return;
140139
}
141140

141+
switch ( $library ) {
142+
case 'ChartJS':
143+
self::set_defaults_chartjs( $chart, $post_status );
144+
break;
145+
case 'GoogleCharts':
146+
self::set_defaults_google( $chart, $post_status );
147+
break;
148+
}
149+
}
150+
151+
/**
152+
* Sets some defaults in the chart for Google charts.
153+
*
154+
* @since 3.3.0
155+
*
156+
* @access private
157+
*/
158+
private static function set_defaults_google( $chart, $post_status ) {
159+
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
160+
161+
$attributes = array();
162+
if ( $post_status === 'auto-draft' ) {
163+
switch ( $type ) {
164+
case 'combo':
165+
// chart type 'bars' and first series 'line'.
166+
$attributes['seriesType'] = 'bars';
167+
$attributes['series'][0]['type'] = 'line';
168+
break;
169+
}
170+
}
171+
172+
if ( $attributes ) {
173+
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
174+
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, array_merge( $settings, $attributes ) );
175+
}
176+
}
177+
178+
/**
179+
* Sets some defaults in the chart for ChartJS charts.
180+
*
181+
* @since 3.3.0
182+
*
183+
* @access private
184+
*/
185+
private static function set_defaults_chartjs( $chart, $post_status ) {
186+
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
187+
$series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
188+
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
189+
190+
$attributes = array();
142191
$name = 'series';
143192
$count = count( $series );
144193
$max = $count - 1;
145-
$attributes = array();
146194
switch ( $type ) {
147195
case 'polarArea':
148196
// fall through.
@@ -170,10 +218,10 @@ public static function set_defaults( $chart, $post_status = 'auto-draft' ) {
170218
}
171219
break;
172220
}
221+
173222
if ( $attributes ) {
174223
$settings[ $name ] = $attributes;
175224
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, $settings );
176225
}
177226
}
178-
179227
}

0 commit comments

Comments
 (0)