Skip to content
Snippets Groups Projects
Commit 758cce4a authored by andileco's avatar andileco
Browse files

Created a ChartsInterface as a place to define constants. Also moved the API...

Created a ChartsInterface as a place to define constants. Also moved the API example module into the modules folder. And fixed code formatting in some places.
parent 40310bf3
No related branches found
No related tags found
No related merge requests found
Showing
with 59 additions and 44 deletions
<?php
use Drupal\charts\Theme\ChartsInterface;
/**
* @file
* Documentation on hooks provided by the Charts module.
......@@ -161,7 +163,7 @@ function hook_charts_type_info() {
// If this chart supports both an X and Y axis, set this to
// CHARTS_DUAL_AXIS. If only a single axis is supported (e.g. pie), then
// set this to CHARTS_SINGLE_AXIS.
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
// Many charting libraries always refer to the main axis as the "y-axis",
// even if the chart's main axis is horizontal. An example of this is a
// bar chart, where the values are along the horizontal axis.
......
......@@ -4,6 +4,7 @@
* @file
* Menu callbacks for Charts module.
*/
use Drupal\charts\Theme\ChartsInterface;
/**
* Module settings page. Users can set the default layout of their charts.
......@@ -23,23 +24,6 @@
* The form with the chart settings added.
*/
/**
* Used to define a single axis.
*
* Constant used in hook_charts_type_info() to declare chart types with a single
* axis. For example a pie chart only has a single dimension.
*/
define('CHARTS_SINGLE_AXIS', 'y_only');
/**
* Used to define a dual axis.
*
* Constant used in hook_charts_type_info() to declare chart types with a dual
* axes. Most charts use this type of data, meaning multiple categories each
* have multiple values. This type of data is usually represented as a table.
*/
define('CHARTS_DUAL_AXIS', 'xy');
/**
* Retrieve a list of all charting libraries available.
*
......@@ -74,7 +58,7 @@ function charts_type_info() {
foreach ($charts_type_info as $chart_type => $chart_type_info) {
$charts_type_info[$chart_type] += array(
'label' => '',
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
'axis_inverted' => FALSE,
'stacking' => FALSE,
);
......@@ -104,31 +88,31 @@ function charts_get_type($chart_type) {
function charts_charts_type_info() {
$chart_types['pie'] = array(
'label' => t('Pie'),
'axis' => CHARTS_SINGLE_AXIS,
'axis' => ChartsInterface::CHARTS_SINGLE_AXIS,
);
$chart_types['bar'] = array(
'label' => t('Bar'),
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
'axis_inverted' => TRUE,
'stacking' => TRUE,
);
$chart_types['column'] = array(
'label' => t('Column'),
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
'stacking' => TRUE,
);
$chart_types['line'] = array(
'label' => t('Line'),
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
);
$chart_types['area'] = array(
'label' => t('Area'),
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
'stacking' => TRUE,
);
$chart_types['scatter'] = array(
'label' => t('Scatter'),
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
);
return $chart_types;
}
......@@ -265,7 +249,7 @@ function charts_settings_form($form, $defaults = array(), $field_options = array
if ($chart_type_info['axis_inverted']) {
$form['type'][$chart_type]['#attributes']['data-axis-inverted'] = TRUE;
}
if ($chart_type_info['axis'] === CHARTS_SINGLE_AXIS) {
if ($chart_type_info['axis'] === ChartsInterface::CHARTS_SINGLE_AXIS) {
$form['type'][$chart_type]['#attributes']['data-axis-single'] = TRUE;
}
}
......
name: charts_api_example
name: Charts API Example
type: module
description: Demonstrates how to use the Charts module without Views.
core: 8.x
......
......@@ -15,7 +15,7 @@ class ChartsApiExample extends ControllerBase implements ContainerInjectionInter
$this->chartSettings = $chartSettings->getChartsSettings();
}
public function display(){
public function display() {
$library = $this->chartSettings['library'];
$options = [];
......@@ -26,23 +26,24 @@ class ChartsApiExample extends ControllerBase implements ContainerInjectionInter
$options['yaxis_max'] = '';
$options['xaxis_title'] = t('X-Axis');
//sample data format
$categories = ["Category 1","Category 2","Category 3","Category 4"];
$categories = ["Category 1", "Category 2", "Category 3", "Category 4"];
$seriesData = [
["name" => "Series 1", "color" => "#0d233a", "type" => null, "data" => [250, 350, 400, 200]],
["name" => "Series 2", "color" => "#8bbc21", "type" => "column", "data" => [150, 450, 500, 300]],
["name" => "Series 3", "color" => "#910000", "type" => "area", "data" => [0, 0, 60, 90]]
["name" => "Series 3", "color" => "#910000", "type" => "area", "data" => [0, 0, 60, 90]]
];
$element = array(
$element = [
'#theme' => 'charts_api_example',
'#library' => $this->t($library),
'#categories' => $categories,
'#seriesData' => $seriesData,
'#options' => $options,
);
];
return $element;
}
public static function create(ContainerInterface $container){
public static function create(ContainerInterface $container) {
return new static(
$container->get('charts.settings')
);
......
......@@ -10,7 +10,7 @@ use Drupal\simpletest\WebTestBase;
*
* @group charts_api_example
*/
class LoadTest extends WebTestBase{
class LoadTest extends WebTestBase {
/**
* Modules to enable.
......
......@@ -2,6 +2,7 @@
namespace Drupal\charts\Form;
use Drupal\charts\Theme\ChartsInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Url;
use Drupal\Core\Link;
......@@ -188,7 +189,7 @@ class ChartsConfigForm extends ConfigFormBase {
if ($chart_type_info['axis_inverted']) {
$form['type'][$chart_type]['#attributes']['data-axis-inverted'] = TRUE;
}
if ($chart_type_info['axis'] === CHARTS_SINGLE_AXIS) {
if ($chart_type_info['axis'] === ChartsInterface::CHARTS_SINGLE_AXIS) {
$form['type'][$chart_type]['#attributes']['data-axis-single'] = TRUE;
}
}
......@@ -477,31 +478,31 @@ class ChartsConfigForm extends ConfigFormBase {
public function charts_charts_type_info() {
$chart_types['pie'] = [
'label' => $this->t('Pie'),
'axis' => CHARTS_SINGLE_AXIS,
'axis' => ChartsInterface::CHARTS_SINGLE_AXIS,
];
$chart_types['bar'] = [
'label' => $this->t('Bar'),
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
'axis_inverted' => TRUE,
'stacking' => TRUE,
];
$chart_types['column'] = [
'label' => $this->t('Column'),
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
'stacking' => TRUE,
];
$chart_types['line'] = [
'label' => $this->t('Line'),
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
];
$chart_types['area'] = [
'label' => $this->t('Area'),
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
'stacking' => TRUE,
];
$chart_types['scatter'] = [
'label' => $this->t('Scatter'),
'axis' => CHARTS_DUAL_AXIS,
'axis' => ChartsInterface::CHARTS_DUAL_AXIS,
];
return $chart_types;
......
......@@ -4,16 +4,16 @@ namespace Drupal\charts\Services;
use Drupal\Core\Config\ConfigFactory;
class ChartsSettingsService implements ChartsSettingsServiceInterface{
class ChartsSettingsService implements ChartsSettingsServiceInterface {
//private $editableConfigName = 'charts.settings';
private $configFactory;
public function __construct(ConfigFactory $config_factory){
public function __construct(ConfigFactory $config_factory) {
$this->configFactory = $config_factory;
}
public function getChartsSettings(){
public function getChartsSettings() {
$config = $this->configFactory->getEditable('charts.settings');
return $config->get('charts_default_settings');
......
<?php
namespace Drupal\charts\Theme;
/**
* Provides an interface defining Charts constants.
*/
interface ChartsInterface {
/**
* Used to define a single axis.
*
* Constant used in hook_charts_type_info() to declare chart types with a single
* axis. For example a pie chart only has a single dimension.
*/
const CHARTS_SINGLE_AXIS = 'y_only';
/**
* Used to define a dual axis.
*
* Constant used in hook_charts_type_info() to declare chart types with a dual
* axes. Most charts use this type of data, meaning multiple categories each
* have multiple values. This type of data is usually represented as a table.
*/
const CHARTS_DUAL_AXIS = 'xy';
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment