Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
charts
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
drupal.org
charts
Commits
242ef85b
Commit
242ef85b
authored
7 years ago
by
Daniel Cothran
Browse files
Options
Downloads
Patches
Plain Diff
Clean up C3 class
parent
a32f7531
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
modules/charts_api_example/src/Controller/ChartsApiExample.php
+1
-0
1 addition, 0 deletions
...es/charts_api_example/src/Controller/ChartsApiExample.php
modules/charts_c3/src/Plugin/chart/C3.php
+72
-58
72 additions, 58 deletions
modules/charts_c3/src/Plugin/chart/C3.php
with
73 additions
and
58 deletions
modules/charts_api_example/src/Controller/ChartsApiExample.php
+
1
−
0
View file @
242ef85b
...
...
@@ -47,6 +47,7 @@ class ChartsApiExample extends ControllerBase implements ContainerInjectionInter
'yaxis_min'
=>
''
,
'yaxis_max'
=>
''
,
'three_dimensional'
=>
FALSE
,
//'grouping' => TRUE,
];
// Sample data format.
...
...
This diff is collapsed.
Click to expand it.
modules/charts_c3/src/Plugin/chart/C3.php
+
72
−
58
View file @
242ef85b
...
...
@@ -37,11 +37,16 @@ class C3 extends AbstractChart {
* Chart Id.
*/
public
function
buildVariables
(
$options
,
$categories
=
[],
$seriesData
=
[],
$attachmentDisplayOptions
=
[],
&
$variables
,
$chartId
)
{
$noAttachmentDisplays
=
count
(
$attachmentDisplayOptions
)
===
0
;
$types
=
[];
// @todo - make this work for more that one attachment.
for
(
$i
=
1
;
$i
<=
count
(
$attachmentDisplayOptions
);
$i
++
)
{
// Create new instance of CThree.
$c3
=
new
CThree
();
$seriesCount
=
count
(
$seriesData
);
$attachmentCount
=
count
(
$attachmentDisplayOptions
);
$noAttachmentDisplays
=
$attachmentCount
===
0
;
$types
=
[];
for
(
$i
=
1
;
$i
<=
$attachmentCount
;
$i
++
)
{
if
(
$attachmentDisplayOptions
[
$i
-
1
][
'style'
][
'options'
][
'type'
]
==
'column'
)
{
$types
[
$seriesData
[
$i
][
'name'
]]
=
'bar'
;
}
...
...
@@ -50,6 +55,21 @@ class C3 extends AbstractChart {
}
}
// Set the chart type.
$c3Chart
=
new
ChartType
();
$c3Chart
->
setType
(
$options
[
'type'
]);
// Set the chart title.
$c3ChartTitle
=
new
ChartTitle
();
$c3ChartTitle
->
setText
(
$options
[
'title'
]);
$c3
->
setTitle
(
$c3ChartTitle
);
// Set up the chart data object.
$chartData
=
new
ChartData
();
$chartData
->
setType
(
$options
[
'type'
]);
$c3
->
setData
(
$chartData
);
$chartAxis
=
new
ChartAxis
();
/**
* For pie and donut chart types, depending on the number of data fields,
* the charts will either use data fields or label fields for the
...
...
@@ -58,12 +78,17 @@ class C3 extends AbstractChart {
* they will become the categories.
* */
if
(
$options
[
'type'
]
==
'pie'
||
$options
[
'type'
]
==
'donut'
)
{
if
(
count
(
$seriesData
)
>
1
)
{
// Set the charts colors.
$chartColor
=
new
ChartColor
();
$seriesColors
=
[];
if
(
$seriesCount
>
1
)
{
$c3Data
=
[];
for
(
$i
=
0
;
$i
<
count
(
$series
Data
)
;
$i
++
)
{
for
(
$i
=
0
;
$i
<
$series
Count
;
$i
++
)
{
$c3DataTemp
=
$seriesData
[
$i
][
'data'
];
array_unshift
(
$c3DataTemp
,
$seriesData
[
$i
][
'name'
]);
array_push
(
$c3Data
,
$c3DataTemp
);
$seriesColor
=
$seriesData
[
$i
][
'color'
];
array_push
(
$seriesColors
,
$seriesColor
);
}
}
else
{
$c3Data
=
[];
...
...
@@ -74,26 +99,53 @@ class C3 extends AbstractChart {
}
array_pop
(
$c3Data
);
}
$chartData
->
setColumns
(
$c3Data
);
$chartColor
->
setPattern
(
$seriesColors
);
$c3
->
setColor
(
$chartColor
);
}
else
{
// Set the charts colors.
$chartColor
=
new
ChartColor
();
$seriesColors
=
[];
for
(
$i
=
0
;
$i
<
$seriesCount
;
$i
++
)
{
$seriesColor
=
$seriesData
[
$i
][
'color'
];
array_push
(
$seriesColors
,
$seriesColor
);
}
$chartColor
->
setPattern
(
$seriesColors
);
$c3
->
setColor
(
$chartColor
);
// Set up the chart data object.
$c3Data
=
[];
for
(
$i
=
0
;
$i
<
count
(
$series
Data
)
;
$i
++
)
{
for
(
$i
=
0
;
$i
<
$series
Count
;
$i
++
)
{
$c3DataTemp
=
$seriesData
[
$i
][
'data'
];
array_unshift
(
$c3DataTemp
,
$seriesData
[
$i
][
'name'
]);
array_push
(
$c3Data
,
$c3DataTemp
);
}
// C3 does not use bar, so column must be used.
if
(
$options
[
'type'
]
==
'bar'
)
{
$chartAxis
->
setRotated
(
TRUE
);
array_unshift
(
$categories
,
'x'
);
array_push
(
$c3Data
,
$categories
);
$chartData
->
setColumns
(
$c3Data
);
}
elseif
(
$options
[
'type'
]
==
'column'
)
{
$chartData
->
setType
(
'bar'
);
$chartAxis
->
setRotated
(
FALSE
);
array_unshift
(
$categories
,
'x'
);
array_push
(
$c3Data
,
$categories
);
$chartData
->
setColumns
(
$c3Data
);
}
else
{
array_unshift
(
$categories
,
'x'
);
array_push
(
$c3Data
,
$categories
);
$chartData
->
setColumns
(
$c3Data
);
}
$c3
->
setAxis
(
$chartAxis
);
}
// Set the chart types.
$chartData
->
types
=
$types
;
$c3Chart
=
new
ChartType
();
$c3Chart
->
setType
(
$options
[
'type'
]);
$c3ChartTitle
=
new
ChartTitle
();
$c3ChartTitle
->
setText
(
$options
[
'title'
]);
$chartAxis
=
new
ChartAxis
();
$c3
=
new
CThree
();
$bindTo
=
'#'
.
$chartId
;
$c3
->
setBindTo
(
$bindTo
);
$c3
->
setTitle
(
$c3ChartTitle
);
$chartData
=
new
ChartData
();
// Set labels to FALSE if has attachments.
if
(
$noAttachmentDisplays
>
0
)
{
$chartData
->
setLabels
(
FALSE
);
}
...
...
@@ -110,55 +162,17 @@ class C3 extends AbstractChart {
$chartAxis
->
y2
=
$showSecAxis
;
}
// Sets the chart type.
$chartData
->
setType
(
$options
[
'type'
]);
$c3
->
setData
(
$chartData
);
if
(
$options
[
'type'
]
==
'bar'
)
{
$chartAxis
->
setRotated
(
TRUE
);
array_unshift
(
$categories
,
'x'
);
array_push
(
$c3Data
,
$categories
);
$chartData
->
setColumns
(
$c3Data
);
}
elseif
(
$options
[
'type'
]
==
'column'
)
{
$chartData
->
setType
(
'bar'
);
$chartAxis
->
setRotated
(
FALSE
);
array_unshift
(
$categories
,
'x'
);
array_push
(
$c3Data
,
$categories
);
$chartData
->
setColumns
(
$c3Data
);
}
elseif
(
$options
[
'type'
]
==
'pie'
||
$options
[
'type'
]
==
'donut'
)
{
$chartData
->
setColumns
(
$c3Data
);
}
else
{
array_unshift
(
$categories
,
'x'
);
array_push
(
$c3Data
,
$categories
);
$chartData
->
setColumns
(
$c3Data
);
}
$chartData
->
types
=
$types
;
if
(
$options
[
'type'
]
!=
'pie'
&&
$options
[
'type'
]
!=
'donut'
)
{
$c3
->
setAxis
(
$chartAxis
);
}
// Determines if chart is stacked.
if
(
!
empty
(
$options
[
'grouping'
]
&&
$options
[
'grouping'
]
==
TRUE
))
{
$seriesNames
=
[];
for
(
$i
=
0
;
$i
<
count
(
$series
Data
)
;
$i
++
)
{
for
(
$i
=
0
;
$i
<
$series
Count
;
$i
++
)
{
array_push
(
$seriesNames
,
$seriesData
[
$i
][
'name'
]);
}
$chartData
->
setGroups
([
$seriesNames
]);
}
$chartColor
=
new
ChartColor
();
$seriesColors
=
[];
for
(
$i
=
0
;
$i
<
count
(
$seriesData
);
$i
++
)
{
$seriesColor
=
$seriesData
[
$i
][
'color'
];
array_push
(
$seriesColors
,
$seriesColor
);
}
$chartColor
->
setPattern
(
$seriesColors
);
$c3
->
setColor
(
$chartColor
);
$bindTo
=
'#'
.
$chartId
;
$c3
->
setBindTo
(
$bindTo
);
$variables
[
'chart_type'
]
=
'c3'
;
$variables
[
'content_attributes'
][
'data-chart'
][]
=
json_encode
(
$c3
);
$variables
[
'attributes'
][
'id'
][
0
]
=
$chartId
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment