Skip to content

Commit

Permalink
update: polish base chart
Browse files Browse the repository at this point in the history
  • Loading branch information
Koooooo-7 committed Jun 26, 2024
1 parent 1a7baa6 commit db284ba
Show file tree
Hide file tree
Showing 21 changed files with 261 additions and 192 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ bin/
.DS_Store
/.idea/
*.html
/echarts4j-snapshot-playwright/


16 changes: 15 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import java.text.SimpleDateFormat

plugins {
id 'java'
id 'maven-publish'
}

group 'com.github.koooooo7'
version '1.0.0-SNAPSHOT'
version project.echarts4jVersion
description ':doughnut: An inevitable charts library for Java.'

ext {
Expand Down Expand Up @@ -38,6 +40,18 @@ dependencies {

}

jar {
manifest {
attributes(
'Built-By': 'KoyZhuang',
'Build-LICENSE': 'MIT',
'Build-Version': project.echarts4jVersion,
'Build-Timestamp': new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()),
)
}
}


test {
useJUnitPlatform()
}
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echarts4jVersion=1.0.0-SNAPSHOT
140 changes: 140 additions & 0 deletions src/main/java/com/github/koooooo7/echarts4j/chart/BaseChart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.github.koooooo7.echarts4j.chart;

import com.github.koooooo7.echarts4j.exception.ChartException;
import com.github.koooooo7.echarts4j.option.ChartOption;
import com.github.koooooo7.echarts4j.option.series.SeriesOption;
import com.github.koooooo7.echarts4j.type.FuncStr;
import com.github.koooooo7.echarts4j.util.ChartUtil;
import com.github.koooooo7.echarts4j.util.JsonUtil;
import com.github.koooooo7.echarts4j.util.annotation.Attention;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
@SuppressWarnings("unchecked")
public abstract class BaseChart<T extends Chart<T>> extends Container implements Chart<T> {
private String chartId;
private ChartOption options;
private final List<String> functions = new LinkedList<>();
private final List<Listener> listeners = new LinkedList<>();

@Override
public T addJSFunction(FuncStr funcStr) {
functions.add(JsonUtil.writeValueAsString(funcStr));
return (T) this;
}

@Override
public T addListener(String eventName, FuncStr handler) {
listeners.add(new Listener(eventName, JsonUtil.writeValueAsString(handler)));
return (T) this;
}

@Override
public T addListener(String eventName, FuncStr query, FuncStr handler) {
listeners.add(new Listener(eventName, JsonUtil.writeValueAsString(query), JsonUtil.writeValueAsString(handler)));
return (T) this;
}

@Attention("Exposed for template and options expert")
@Override
public String getOptions() {
return JsonUtil.writeValueAsString(options);
}

@Override
public ChartOption getChartOptions() {
return this.options;
}

@Override
public Chart<T> overlap(Chart<?> c) {
final ChartOption chartOptions = c.getChartOptions();
final List<SeriesOption> series = chartOptions.getSeries();
options.getSeries().addAll(series);
return this;
}

@Override
public void postProcessor() {
configChartIdIfNecessary();
configChartTypeOnSeriesIfNecessary();
injectInstanceToFunctionsIfNecessary();
injectInstanceToListenersIfNecessary();
}

private void configChartIdIfNecessary() {
if (StringUtils.isEmpty(chartId)) {
chartId = ChartUtil.generateChartId();
}
this.setContainerId(this.chartId);
}

private void configChartTypeOnSeriesIfNecessary() {
if (Objects.nonNull(options)) {
options.getSeries().forEach(s -> {
if (StringUtils.isEmpty(s.getType())) {
if (ChartType.Generic == this.getChartType() || ChartType.BASE == this.getChartType()) {
throw new ChartException("Please set the chart " +
"type in series or use the specific Chart builder rather Generic/Base chart");
}
s.setType(this.getChartType().getType());
}
});
}
}

private void injectInstanceToFunctionsIfNecessary() {
if (listeners.isEmpty()) {
return;
}

listeners.forEach(l -> l.setHandler(ChartUtil.injectInstance(l.getHandler(), this)));
}

private void injectInstanceToListenersIfNecessary() {
if (functions.isEmpty()) {
return;
}

final List<String> tmp = functions
.stream()
.map(f -> ChartUtil.injectInstance(f, this))
.collect(Collectors.toList());
functions.clear();
functions.addAll(tmp);
}

@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class Listener {
private String eventName;
private String query;
private String handler;

public Listener(String eventName, String handler) {
this.eventName = eventName;
this.handler = handler;
}

public Listener(String eventName, String query, String handler) {
this.eventName = eventName;
this.query = query;
this.handler = handler;
}
}

}


47 changes: 32 additions & 15 deletions src/main/java/com/github/koooooo7/echarts4j/chart/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@


import com.github.koooooo7.echarts4j.exception.ChartException;
import com.github.koooooo7.echarts4j.exception.RenderException;
import com.github.koooooo7.echarts4j.render.Render;
import com.github.koooooo7.echarts4j.render.RenderProvider;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -26,27 +24,40 @@ public class Canvas {
private String echartsAsset = "https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js";
private List<String> appendJsAssets = new ArrayList<>();
private List<String> appendCssAssets = new ArrayList<>();
private final Map<String, Chart> charts = new LinkedHashMap<>();
private final Map<String, Chart<?>> charts = new LinkedHashMap<>();

Canvas() {
}

// A handy method to quick render out

/**
* A handy method to quick render out charts.
*
* @param file the {@link File} locate.
* @return the {@link Canvas} instance for further operations.
*/
public Canvas renderTo(File file) {
try (Writer writer = new FileWriter(file)) {
RenderProvider.get().render(this, writer);
return this;
} catch (Exception e) {
throw new RenderException(e);
}
RenderProvider.get().render(this, file);
return this;
}

/**
* A handy method to quick render out charts to different output.
*
* @param writer the {@link Writer}, such as {@link java.io.FileWriter}, {@link java.io.StringWriter}.
* @return the {@link Canvas} instance for further operations.
*/
public Canvas renderTo(Writer writer) {
RenderProvider.get().render(this, writer);
return this;
}

// one way ticket

/**
* A one way step to extract the {@link Render}, same to use {@see RenderProvider#get()}.
*
* @return the {@link Render} instance head of the render chain.
*/
public Render extractRender() {
return RenderProvider.get();
}
Expand All @@ -55,7 +66,7 @@ public CanvasBuilder asBuilder() {
return new CanvasBuilder(this);
}

private void registerChart(String chartId, Chart chart) {
private void registerChart(String chartId, Chart<?> chart) {
charts.putIfAbsent(chartId, chart);
}

Expand Down Expand Up @@ -91,20 +102,26 @@ public CanvasBuilder replaceEchartsAsset(String echartsAsset) {
return this;
}

public CanvasBuilder addCharts(Chart... charts) {
public CanvasBuilder addCharts(Chart<?>... charts) {
Arrays.stream(charts).forEach(c -> {
c.postProcessor();
if (Objects.isNull(c.getChartType())) {
throw new ChartException("Can not add a no type chart !");
}
c.postProcessor();
canvas.registerChart(c.getChartId(), c);
});

return this;
}

/**
* @param chartId the chartId which user preset on {@link Chart} when addChart.
* @param chartModifier the chartModifier to update the Chart.
* @param <T> the Chart instance type.
* @return the {@link CanvasBuilder} to do further operations.
*/
@SuppressWarnings("unchecked")
public <T extends Chart> CanvasBuilder updateChart(String chartId, Consumer<Optional<T>> chartModifier) {
public <T extends Chart<T>> CanvasBuilder updateChart(String chartId, Consumer<Optional<T>> chartModifier) {
chartModifier.accept(Optional.ofNullable((T) (canvas.getCharts().get(chartId))));
return this;
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/github/koooooo7/echarts4j/chart/Chart.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.koooooo7.echarts4j.chart;

import com.github.koooooo7.echarts4j.option.ChartOption;
import com.github.koooooo7.echarts4j.type.FuncStr;

public interface Chart {
public interface Chart<T> {

ChartType getChartType();

Expand All @@ -12,7 +13,13 @@ public interface Chart {

String getChartId();

Chart overlap(Chart c);
Chart<T> overlap(Chart<?> c);

T addJSFunction(FuncStr funcStr);

T addListener(String eventName, FuncStr handler);

T addListener(String eventName, FuncStr query, FuncStr handler);

void postProcessor();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
@AllArgsConstructor
@Getter
public enum ChartType {
/**
* The base abstract chart definition.
*/
BASE(StringUtils.EMPTY),
/**
* The generic chart with full customize.
*/
Generic(StringUtils.EMPTY),
Line("line"),
Bar("bar"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.experimental.SuperBuilder;

@Data
@SuperBuilder()
@SuperBuilder
public class Container {
private String containerId;
@Builder.Default
Expand Down
Loading

0 comments on commit db284ba

Please sign in to comment.