Skip to content

Commit

Permalink
Allow setting global options
Browse files Browse the repository at this point in the history
Fixes #508
Fixes #472
  • Loading branch information
alvarezguille committed Mar 28, 2018
1 parent 2ee4d61 commit 1162292
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
26 changes: 25 additions & 1 deletion addon/src/main/java/com/vaadin/addon/charts/ChartOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Iterator;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.vaadin.addon.charts.model.Global;
import com.vaadin.addon.charts.model.Lang;
import com.vaadin.addon.charts.model.style.Theme;
import com.vaadin.addon.charts.shared.ChartOptionsState;
Expand All @@ -45,6 +46,8 @@ public class ChartOptions extends AbstractExtension {

private Lang lang;

private Global global;

protected ChartOptions() {
}

Expand Down Expand Up @@ -99,6 +102,27 @@ public Theme getTheme() {
return theme;
}

/**
* Changes the Global params of all charts.
*
* @param global
*/
public void setGlobal(Global global) {
this.global = global;
buildOptionsJson();
notifyListeners();
}

/**
* Returns the {@link Global} in use or {@code null} if no lang
* configuration has been set.
*
* @return the {@link Global} in use or {@code null}.
*/
public Global getGlobal() {
return global;
}

/**
* Changes the language of all charts.
*
Expand Down Expand Up @@ -174,4 +198,4 @@ public static ChartOptions get() {
return get(ui);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ public List<BeanPropertyWriter> changeProperties(
List<BeanPropertyWriter> beanProperties) {

if (ChartOptions.class.isAssignableFrom(beanDesc.getBeanClass())) {
// make sure we only serialize the lang and theme fields from
// make sure we only serialize the lang, theme and global fields
// from
// ChartOptions, otherwise everything from parent would be
// serialized
List<BeanPropertyWriter> newProperties = new ArrayList<BeanPropertyWriter>();

for (BeanPropertyWriter writer : beanProperties) {
if ("lang" == writer.getName() || "theme" == writer.getName()) {
if ("lang" == writer.getName() || "theme" == writer.getName()
|| "global" == writer.getName()) {
newProperties.add(writer);
}
}
Expand Down Expand Up @@ -80,4 +82,4 @@ public JsonSerializer<?> modifySerializer(SerializationConfig config,
return super.modifySerializer(config, beanDesc, serializer);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package com.vaadin.addon.charts.model.junittests;

import static com.vaadin.addon.charts.util.ChartSerialization.toJSON;
import static org.junit.Assert.assertEquals;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.addon.charts.ChartOptions;
import com.vaadin.addon.charts.model.DataSeries;
import com.vaadin.addon.charts.model.DataSeriesItem;
import com.vaadin.addon.charts.model.Global;
import com.vaadin.addon.charts.model.Lang;
import com.vaadin.addon.charts.model.style.GradientColor;
import com.vaadin.addon.charts.model.style.SolidColor;
import com.vaadin.addon.charts.model.style.Theme;
import com.vaadin.addon.charts.util.ChartSerialization;
import com.vaadin.ui.UI;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.IOException;

import static com.vaadin.addon.charts.util.ChartSerialization.toJSON;
import static org.junit.Assert.assertEquals;

@RunWith(MockitoJUnitRunner.class)
public class ChartOptionsJSONSerializationTest {
Expand Down Expand Up @@ -54,6 +56,15 @@ public void toJSON_ThemeSet_ThemeSerialized() {
assertEquals(EmptyThemeJson, toJSON(options));
}

@Test
public void toJSON_GlobalSet_GlobalSerialized() {
Global global = new Global();
global.setUseUTC(false);
options.setGlobal(global);

assertEquals("{\"global\":{\"useUTC\":false}}", toJSON(options));
}

@Test
public void toJSON_ThemeSetWithLinearGradient_ThemeSerializedWithLinearGradient() {
Theme theme = new Theme();
Expand Down Expand Up @@ -151,4 +162,4 @@ public void toJSON_itemWithRadialGradientColor_RadialGradientSerialized() {
assertEquals(expected, toJSON(series));
}

}
}

0 comments on commit 1162292

Please sign in to comment.