Participation::get_chart_json( awsmug\Torro_Forms\DB_Objects\Forms\Form $form, string $id, array $x_values, array $y_values, string $x_label, string $y_label )

Returns the JSON data to generate the chart.

Description

See also

Parameters

$form

(awsmug\Torro_Forms\DB_Objects\Forms\Form) (Required) Form object.

$id

(string) (Required) ID attribute of the element to bind the chart to.

$x_values

(array) (Required) Values for the 'x' axis.

$y_values

(array) (Required) Values for the 'y' axis. Must match the number of $x_values passed.

$x_label

(string) (Required) Label for the 'x' axis.

$y_label

(string) (Required) Label for the 'y' axis.

Return

(array) JSON data for the chart.

Source

File: src/modules/evaluators/participation.php

	protected function get_chart_json( $form, $id, $x_values, $y_values, $x_label, $y_label ) {
		global $wp_locale;

		$display_mode      = $this->get_form_option( $form->id, 'display_mode', 'line' );
		$data_point_labels = $this->get_form_option( $form->id, 'data_point_labels', 'none' );

		$less_than_10 = array_reduce( $y_values, function( $carry, $y_value ) {
			if ( $y_value > 10 ) {
				return false;
			}

			return $carry;
		}, true );

		$labels = false;
		if ( 'value' === $data_point_labels ) {
			$labels = true;
		} elseif ( 'percentage' === $data_point_labels ) {
			$aggregate = array_reduce( $y_values, function( $carry, $y_value ) {
				$carry += $y_value;

				return $carry;
			}, 0 );

			$labels = array(
				'format' => array(
					'template'  => '%percentage%%',
					'aggregate' => $aggregate,
				),
			);
		}

		array_unshift( $x_values, 'x' );
		array_unshift( $y_values, 'submissionCount' );

		$data = array(
			'bindto' => '#' . $id,
			'data'   => array(
				'x'       => 'x',
				'columns' => array( $x_values, $y_values ),
				'names'   => array(
					'submissionCount' => $y_label,
				),
				'type'    => $display_mode,
				'colors'  => array(
					'submissionCount' => '#0073aa',
				),
				'labels'  => $labels,
			),
			'axis'   => array(
				'x' => array(
					'label' => $x_label,
					'type' => 'category',
				),
				'y' => array(
					'label' => $y_label,
					'min'   => 1,
				),
			),
			'legend' => array(
				'show' => false,
			),
		);
		if ( $less_than_10 ) {
			$data['axis']['y']['max'] = 10;
		}

		return $data;
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.