Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding colors for axes. #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/block-src/chart/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default function edit( { attributes, setAttributes } ) {
<h4 className="title">{ selected.title }</h4>
</div>
:
<img className="preview" src={ selected?.src + random } />
<img className="preview" src={ selected?.src + random } alt="chart" />
}
</div>
:
Expand Down
2 changes: 1 addition & 1 deletion components/class-m-chart-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ class="hide" />
<input type="hidden" name="set_name" value=""
id="<?php echo esc_attr( $this->get_field_id( 'csv-set-name' ) ); ?>" />
</form>
<script type="text/javascript">
<script>
<?php do_action( 'm_chart_admin_footer_javascript' ); ?>
</script>
<?php
Expand Down
9 changes: 9 additions & 0 deletions components/class-m-chart-chartjs.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,9 @@ public function add_axis_labels( $chart_args ) {
'display' => '' == $this->post_meta['x_title'] ? false : true,
'text' => $this->esc_title( $this->post_meta['x_title'] ),
);
$chart_args['options']['scales']['x']['ticks'] = array(
'color' => esc_attr($this->post_meta['x_axis_color']),
);

// We've got x axis units so we'll add them to the axis label
if ( '' != $this->post_meta['x_units'] ) {
Expand All @@ -574,6 +577,12 @@ public function add_axis_labels( $chart_args ) {
$chart_args['options']['scales']['y']['title']['text'] .= $y_units;
}

// Color of text
$chart_args['options']['scales']['y']['ticks'] = array(
'color' => esc_attr($this->post_meta['y_axis_color']),
);


return $chart_args;
}

Expand Down
4 changes: 3 additions & 1 deletion components/class-m-chart.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

class M_Chart {
public $version = '1.11.1';
public $version = '1.12.1';
public $slug = 'm-chart';
public $plugin_name = 'Chart';
public $chart_meta_fields = array(
Expand All @@ -12,10 +12,12 @@ class M_Chart {
'shared' => false,
'subtitle' => '',
'y_title' => '',
'y_axis_color'=> '#000000',
'y_units' => '',
'y_min' => false,
'y_min_value' => 0,
'x_title' => '',
'x_axis_color'=> '#000000',
'x_units' => '',
'height' => 400,
'legend' => true,
Expand Down
24 changes: 20 additions & 4 deletions components/css/m-chart-admin.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/js/m-chart-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
this.nonce = $( 'input[name="m-chart[nonce]"]' ).attr( 'value' );

// Store the setting inputs and title input for use later
this.$setting_inputs = $( document.getElementById( 'm-chart' ) ).find( '.settings input, .settings select' );
this.$setting_inputs = $( document.getElementById( 'm-chart' ) ).find( '.settings input, .settings select, .axis-color input' );
this.$title_input = $( document.getElementById( 'titlewrap' ) ).find( 'input' );
this.$subtitle_input = $( document.getElementById( 'titlediv' ) ).find( '#m-chart-subtitle' );
this.$y_min_value = $( document.getElementById( 'm-chart-y-min-value' ) );
Expand Down
129 changes: 66 additions & 63 deletions components/templates/chartjs-chart.php
Original file line number Diff line number Diff line change
@@ -1,80 +1,83 @@
<?php
$title = get_the_title( $post_id );
$height = m_chart()->get_post_meta( $post_id, 'height' );
$width = '';
$title = get_the_title($post_id);
$height = m_chart()->get_post_meta($post_id, 'height');
$width = '';

$subtitle = m_chart()->get_post_meta( $post_id, 'subtitle' );
$subtitle = m_chart()->get_post_meta($post_id, 'subtitle');

if ( '' != $subtitle ) {
$title = $title . ': ' . $subtitle;
if ('' != $subtitle) {
$title = $title . ': ' . $subtitle;
}

if ( '' != $args['width'] && 'responsive' != $args['width'] ) {
$width = ' width="' . absint( $args['width'] ) . '"';
if ('' != $args['width'] && 'responsive' != $args['width']) {
$width = ' width="' . absint($args['width']) . '"';
}
?>
<div id="m-chart-container-<?php echo absint( $post_id ); ?>-<?php echo absint( $this->instance ); ?>" class="m-chart-container chartjs">
<canvas id="m-chart-<?php echo absint( $post_id ); ?>-<?php echo absint( $this->instance ); ?>" class="m-chart" height="<?php echo absint( $height ); ?>"<?php echo $width; ?> aria-label="<?php echo esc_attr( $title ); ?>" role="img" style="height: <?php echo esc_attr( $height ); ?>px;"></canvas>
<div id="m-chart-container-<?php echo absint($post_id); ?>-<?php echo absint($this->instance); ?>"
class="m-chart-container chartjs">
<canvas id="m-chart-<?php echo absint($post_id); ?>-<?php echo absint($this->instance); ?>" class="m-chart"
height="<?php echo absint($height); ?>"<?php echo $width; ?> aria-label="<?php echo esc_attr($title); ?>"
role="img" style="height: <?php echo esc_attr($height); ?>px;"></canvas>
</div>
<script type="text/javascript">
var m_chart_container_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>_canvas = document.getElementById( 'm-chart-<?php echo absint( $post_id ); ?>-<?php echo absint( $this->instance ); ?>' ).getContext('2d');
<script>
var m_chart_container_<?php echo absint($post_id); ?>_<?php echo absint($this->instance); ?>_canvas = document.getElementById('m-chart-<?php echo absint($post_id); ?>-<?php echo absint($this->instance); ?>').getContext('2d');

var m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?> = {
chart_args: <?php echo $this->unicode_aware_stripslashes( json_encode( $this->library( 'chartjs' )->get_chart_args( $post_id, $args ), JSON_HEX_QUOT ) ); ?>,
post_id: <?php echo absint( $post_id ); ?>,
instance: <?php echo absint( $this->instance ); ?>,
render_1: true
};
var m_chart_chartjs_<?php echo absint($post_id); ?>_<?php echo absint($this->instance); ?> = {
chart_args: <?php echo $this->unicode_aware_stripslashes(json_encode($this->library('chartjs')->get_chart_args($post_id, $args), JSON_HEX_QUOT)); ?>,
post_id: <?php echo absint($post_id); ?>,
instance: <?php echo absint($this->instance); ?>,
render_1: true
};

<?php do_action( 'm_chart_after_chart_args', $post_id, $args, $this->instance ); ?>
<?php do_action('m_chart_after_chart_args', $post_id, $args, $this->instance); ?>

(function( $ ) {
m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.render_chart = function() {
$( '.m-chart' ).trigger({
type: 'render_start',
post_id: this.post_id,
instance: this.instance
});

var target = this.chart_args.options.animation;

var source = {
onComplete: function() {
// This deals with an issue in Chart.js 3.1.0 where onComplete can run too many times
// We only want to trigger on the first render anyway so we'll just check
if ( false === m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.render_1 ) {
return;
}
(function ($) {
m_chart_chartjs_<?php echo absint($post_id); ?>_<?php echo absint($this->instance); ?>.render_chart = function () {
$('.m-chart').trigger({
type: 'render_start',
post_id: this.post_id,
instance: this.instance
});

m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.render_1 = false;
var target = this.chart_args.options.animation;

$( '.m-chart' ).trigger({
type: 'render_done',
post_id: m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.post_id,
instance: m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.instance
});
}
}

if ( ! target ) {
source = {animation: source};
target = this.chart_args.options;
}

Object.assign( target, source );
var source = {
onComplete: function () {
// This deals with an issue in Chart.js 3.1.0 where onComplete can run too many times
// We only want to trigger on the first render anyway so we'll just check
if (false === m_chart_chartjs_<?php echo absint($post_id); ?>_<?php echo absint($this->instance); ?>.render_1) {
return;
}

Chart.register( ChartDataLabels );
m_chart_chartjs_<?php echo absint($post_id); ?>_<?php echo absint($this->instance); ?>.render_1 = false;

this.chart = new Chart(
m_chart_container_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>_canvas,
this.chart_args
);
};
$('.m-chart').trigger({
type: 'render_done',
post_id: m_chart_chartjs_<?php echo absint($post_id); ?>_<?php echo absint($this->instance); ?>.post_id,
instance: m_chart_chartjs_<?php echo absint($post_id); ?>_<?php echo absint($this->instance); ?>.instance
});
}
}

$( function() {
$.when( m_chart_chartjs_helpers.init() ).done(function() {
m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.render_chart();
});
} );
})( jQuery );
if (!target) {
source = {animation: source};
target = this.chart_args.options;
}

Object.assign(target, source);

Chart.register(ChartDataLabels);

this.chart = new Chart(
m_chart_container_<?php echo absint($post_id); ?>_<?php echo absint($this->instance); ?>_canvas,
this.chart_args
);
};

$(function () {
$.when(m_chart_chartjs_helpers.init()).done(function () {
m_chart_chartjs_<?php echo absint($post_id); ?>_<?php echo absint($this->instance); ?>.render_chart();
});
});
})(jQuery);
</script>
8 changes: 8 additions & 0 deletions components/templates/chartjs-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
?>
</select>
</p>
<p class="axis-color">
<label for="<?php echo esc_attr( $this->get_field_id( 'y-axis-color' ) ); ?>"><?php esc_html_e( 'Vertical axis text color', 'm-chart' ); ?></label><br />
<input class="input" type="color" name="<?php echo esc_attr( $this->get_field_name( 'y_axis-color' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'y-axis-color' ) ); ?>" value="<?php echo esc_attr( $post_meta['y_axis_color'] ); ?>" />
</p>
</div>
<div class="row four y-min">
<p>
Expand Down Expand Up @@ -143,6 +147,10 @@
?>
</select>
</p>
<p class="axis-color">
<label for="<?php echo esc_attr( $this->get_field_id( 'x-axis-color' ) ); ?>"><?php esc_html_e( 'Horizontal axis text color', 'm-chart' ); ?></label><br />
<input class="input" type="color" name="<?php echo esc_attr( $this->get_field_name( 'x_axis-color' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'x-axis-color' ) ); ?>" value="<?php echo esc_attr( $post_meta['x_axis_color'] ); ?>" />
</p>
</div>
</div>
<div class="row seven">
Expand Down