Skip to content

Commit

Permalink
Update of all migrations. Code, interface and structure improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
carderm authored and carderm committed Oct 23, 2020
1 parent edd0baa commit 054d55e
Show file tree
Hide file tree
Showing 16 changed files with 371 additions and 116 deletions.
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ A plugin for DjangoCMS that creates easy to use and fully customisable ChartJs (
- 3.0.0
- **CAUTION** - This is a complete refactoring of DjangoCMS Charts to ChartJS version 2.x
- ***All Models, Fields, and Options have changed***
- ***Due to changes in ChartJS 1.x > 2.x - Custom settings will NOT be migrated***
- The migration attempts to bring over the core settings from each previous version chart.
- **ChartJS-Sass - Deprecated**
- All chart types are now in the ChartsJS Plugin only
- Multiple Datasets can be added below the parent ChartJS Plugin
- ***Due to changes in ChartJS 1.x > 2.x - Not all Custom settings will be migrated***
- The migrations attempt to bring over any settings changed from the default values from each previous chart.
- ChartJS is enabled by default - update settings to disable as below
- All chart types are now available in the ChartsJS Plugin
- Multiple Datasets can be added as Child Plugins of the parent ChartJS Plugin
- Global Options are added in the Admin, as required.
- All Options come from the ChartJS object/dictionary and are added from a Select2 list
- All Options come from the ChartJS object/dictionary and are using a Select2 list

## Quick start
1. Add 'djangocms_charts' to your INSTALLED_APPS setting like this::
Expand Down Expand Up @@ -76,13 +76,13 @@ The JSON view can then be accessed via:
All chart dataset colours (backgroundColor, borderColor, etc) can be set using CSS via ChartJS-Sass. This JS library will update any unspecified colors with those specified in the CSS and built using SASS.
For more details see: https://github.com/mcldev/ChartJS-Sass

1. To enable, in `settings.py` add `DJANGOCMS_CHARTS_ENABLE_CHARTJS_SASS = True`
1. To disable/enable, in `settings.py` add `DJANGOCMS_CHARTS_ENABLE_CHARTJS_SASS = True or False (default=True)`


# Usage
## Usage

## Chart Types
The following chart types can be selected
### Chart Types
The following chart types can be selected with options (see below)
- Line
- Line XY (Scatter with line)
- Bar
Expand All @@ -96,7 +96,7 @@ The following chart types can be selected
- Mixed (see Multiple Datasets)


## Input Data
### Input Data
All input data will be used as below.

NB: Multiple datasets can be added as either:
Expand Down Expand Up @@ -138,19 +138,19 @@ NB: Multiple datasets can be added as either:
# ...
```

## Axes
### Axes
https://www.chartjs.org/docs/latest/axes/

Multiple Axes can be added using X Axis or Y Axis. Each Axis can be used multiple times (e.g. Linear axis).
Options for Axes are set below.

## Multiple Datasets
### Multiple Datasets
https://www.chartjs.org/docs/latest/charts/mixed.html#drawing-order

Multiple datasets can be added as rows/columns of the main chart, or added as Dataset child plugins.
The rendering order for ChartJS is that the first dataset is top-most - this plugin prepends the subsequent child datasets so the last dataset is top-most.

## Mixed Types
### Mixed Types
https://www.chartjs.org/docs/latest/charts/mixed.html

Each child Dataset can have a different type, thus creating a Mixed Chart.
Expand Down Expand Up @@ -206,13 +206,15 @@ https://www.chartjs.org/docs/latest/configuration/
Options are set in JavaScript using the settings provided by ChartJS - Use this documentation: https://www.chartjs.org/docs/latest/
The order of options are:
The Options are assigned in ascending order of priority as:
- `Chart.defaults.global.<option>` - see GlobalOptionsGroup in Admin
- `chart.options.<option>` - see ChartOptions Group selectable for each chart
- `dataset.<option>` - see DatasetOptionsGroup selectable for each dataset
- `chart.options.scales.<axes>.<option>` - see AxisOptionsGroup selectable for each axis
- `chart.options.<option>` - see ChartOptions Group selectable for each chart
- `chart.options.<option>` - see ChartSpecificOptions assigned to each chart individually
- `dataset.<option>` - see DatasetOptionsGroup selectable for each dataset
- `dataset.<option>` - see DatasetSpecificOptions assigned to each dataset individually
- `chart.options.scales.<axes>.<option>` - see AxisOptionsGroup selectable for each axis
## Option Input Types
### Option Input Types
https://www.chartjs.org/docs/latest/general/options.html
ChartJS accepts various input option formats, some can be scripted, functions, numbers, or text.
Expand Down
23 changes: 18 additions & 5 deletions djangocms_charts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
from .forms import GlobalOptionsInlineForm, ChartOptionsInlineForm, DatasetOptionsInlineForm, AxisOptionsInlineForm, \
ColorInputForm
from .models import GlobalOptionsGroupModel, GlobalOptionsModel, \
ChartOptionsGroupModel, ChartOptionsModel,\
ChartOptionsGroupModel, ChartOptionsModel, \
DatasetOptionsGroupModel, DatasetOptionsModel, \
AxisOptionsGroupModel, AxisOptionsModel
AxisOptionsGroupModel, AxisOptionsModel, ChartSpecificOptionsModel, DatasetSpecificOptionsModel
from .models_colors import ColorModel, ColorGroupModel


# Inline Forms for Options
# ------------------------
from .models_colors import ColorModel, ColorGroupModel

# Inline Forms for Options

# ------------------------

class OptionsInlineBase(admin.TabularInline):
fields = ['label', 'type', 'value']
list_display = ('label', 'type', 'value')
extra = 0


# Options Groups Inlines
# ------------------------
class GlobalOptionsInlineAdmin(OptionsInlineBase):
model = GlobalOptionsModel
form = GlobalOptionsInlineForm
Expand All @@ -36,6 +39,16 @@ class AxisOptionsInlineAdmin(OptionsInlineBase):
model = AxisOptionsModel
form = AxisOptionsInlineForm

# Specific Options inlines
# ------------------------
class ChartSpecificOptionsInlineAdmin(OptionsInlineBase):
model = ChartSpecificOptionsModel
form = ChartOptionsInlineForm

class DatasetSpecificOptionsInlineAdmin(OptionsInlineBase):
model = DatasetSpecificOptionsModel
form = DatasetOptionsInlineForm


# Register Options Groups
# ------------------------
Expand Down
21 changes: 14 additions & 7 deletions djangocms_charts/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from djangocms_charts.admin import ChartSpecificOptionsInlineAdmin, DatasetSpecificOptionsInlineAdmin
from djangocms_charts.forms import DatasetInputForm
from djangocms_charts.models import ChartModel, DatasetModel, GlobalOptionsGroupModel

Expand All @@ -20,6 +21,9 @@ class ChartJsPlugin(CMSPluginBase):
allow_children = True
child_classes = ['DatasetPlugin']
form = DatasetInputForm
inlines = [
ChartSpecificOptionsInlineAdmin,
]
fieldsets = (
(None, {
'fields': ('label', 'type', 'caption')
Expand All @@ -34,14 +38,14 @@ class ChartJsPlugin(CMSPluginBase):
'fields': ('color_by_dataset', 'colors')
}),
(_("Chart Options"), {
'fields': ('chart_options', 'options', 'xAxis', 'yAxis')
'fields': ('chart_options_group', 'dataset_options_group', 'xAxis', 'yAxis')
}),
(_("Chart Settings"), {
'fields': ('display_title', 'chart_width', 'chart_height')
'fields': ('display_title', 'display_legend', 'legend_position')
}),
(_("Chart Classes"), {
(_("Advanced Settings and Classes"), {
'classes': ('collapse',),
'fields': ('chart_container_classes', 'chart_classes',)
'fields': ('chart_width', 'chart_height', 'chart_container_classes', 'chart_classes',)
}),
)

Expand All @@ -52,7 +56,7 @@ def render(self, context, instance, placeholder):
chart_json = json.dumps(chart_data)
color_by_dataset = instance.color_by_dataset if instance.color_by_dataset is not None else False
color_by_dataset = json.dumps(color_by_dataset)
enable_chartjs_sass = getattr(settings, 'DJANGOCMS_CHARTS_ENABLE_CHARTJS_SASS', False)
enable_chartjs_sass = getattr(settings, 'DJANGOCMS_CHARTS_ENABLE_CHARTJS_SASS', True)

context.update({
'chart_data': chart_json,
Expand All @@ -71,6 +75,9 @@ class DatasetPlugin(CMSPluginBase):
require_parent = True
parent_classes = ['ChartJsPlugin']
form = DatasetInputForm
inlines = [
DatasetSpecificOptionsInlineAdmin,
]
fieldsets = (
(None, {
'fields': ('label', 'type')
Expand All @@ -84,8 +91,8 @@ class DatasetPlugin(CMSPluginBase):
(_("Dataset Colors"), {
'fields': ('color_by_dataset', 'colors')
}),
(_("Chart Options"), {
'fields': ('options', 'xAxis', 'yAxis')
(_("Datasetl Options"), {
'fields': ('dataset_options_group', 'xAxis', 'yAxis')
}),
)

36 changes: 26 additions & 10 deletions djangocms_charts/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def is_coordinate_type(cls, type):
DATASET_FORMATS = [('rows', 'rows'),
('cols', 'cols'),]

LEGEND_POSITIONS = [('top', 'top'),
('left', 'left'),
('bottom', 'bottom'),
('right', 'right'), ]

OPTION_DATA_TYPES = [('text', 'text'),
('number', 'number'),
('boolean', 'boolean'),
Expand All @@ -48,13 +53,13 @@ def is_coordinate_type(cls, type):
('function', 'function')]

COLOR_LABELS = [('backgroundColor', 'backgroundColor'),
('borderColor', 'borderColor'),
('hoverBackgroundColor', 'hoverBackgroundColor'),
('hoverBorderColor', 'hoverBorderColor'),
('pointBackgroundColor', 'pointBackgroundColor'),
('pointBorderColor', 'pointBorderColor'),
('pointHoverBackgroundColor', 'pointHoverBackgroundColor'),
('pointHoverBorderColor', 'pointHoverBorderColor')]
('borderColor', 'borderColor'),
('hoverBackgroundColor', 'hoverBackgroundColor'),
('hoverBorderColor', 'hoverBorderColor'),
('pointBackgroundColor', 'pointBackgroundColor'),
('pointBorderColor', 'pointBorderColor'),
('pointHoverBackgroundColor', 'pointHoverBackgroundColor'),
('pointHoverBorderColor', 'pointHoverBorderColor')]

# -------------------------------

Expand Down Expand Up @@ -314,6 +319,8 @@ def get_chartjs_global_options():

CHARTJS_CHART_OPTIONS_LOOKUP = {
"options": [
"circumference",
"cutoutPercentage",
"defaultColor",
"defaultFontColor",
"defaultFontFamily",
Expand All @@ -326,10 +333,13 @@ def get_chartjs_global_options():
"onClick",
"responsive",
"responsiveAnimationDuration",
"rotation",
"showLines",
"spanGaps",
],
"options.animation": [
"animation.animateRotate",
"animation.animateScale",
"animation.duration",
"animation.easing",
"animation.onComplete",
Expand Down Expand Up @@ -423,10 +433,14 @@ def get_chartjs_global_options():
"scale.pointLabels.callback",
"scale.pointLabels.display",
"scale.pointLabels.fontSize",
"scale.scaleLabel.padding.bottom",
"scale.scaleLabel.padding.top",
"scale.scaleLabel.display",
"scale.scaleLabel.labelString",
"scale.scaleLabel.lineHeight",
"scale.scaleLabel.fontColor",
"scale.scaleLabel.fontFamily",
"scale.scaleLabel.fontSize",
"scale.scaleLabel.fontStyle",
"scale.scaleLabel.padding",
"scale.ticks.autoSkip",
"scale.ticks.autoSkipPadding",
"scale.ticks.backdropColor",
Expand Down Expand Up @@ -475,7 +489,10 @@ def get_chartjs_global_options():
"tooltips.backgroundColor",
"tooltips.bodyAlign",
"tooltips.bodyFontColor",
"tooltips.bodyFontFamily",
"tooltips.bodyFontSize",
"tooltips.bodySpacing",
"tooltips.bodyFontStyle",
"tooltips.borderColor",
"tooltips.borderWidth",
"tooltips.caretPadding",
Expand Down Expand Up @@ -504,7 +521,6 @@ def get_chartjs_global_options():

}


CACHE_CHART_OPTIONS = []

def get_chartjs_chart_options():
Expand Down
2 changes: 1 addition & 1 deletion djangocms_charts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def clean(self):

class Meta:
labels = {
'options': _('Dataset options'),
'dataset_options_group': _('Dataset options'),
}


Expand Down
7 changes: 5 additions & 2 deletions djangocms_charts/migration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,12 @@ def migrate_cms_plugin(apps, old_table, new_app, new_model, new_plugin_type, fie
val_or_func = value_mapping[old_field][None]
if callable(val_or_func):
try:
new_value = val_or_func(new_value, apps)
new_value = val_or_func(new_value, apps, old_plugin_data)
except:
new_value = val_or_func(new_value)
try:
new_value = val_or_func(new_value, apps)
except:
new_value = val_or_func(new_value)
else:
new_value = val_or_func

Expand Down
Loading

0 comments on commit 054d55e

Please sign in to comment.