Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect work 'withMax' when parameter is smaller than 1 #86

Open
lanmaster opened this issue Dec 5, 2020 · 1 comment
Open

Incorrect work 'withMax' when parameter is smaller than 1 #86

lanmaster opened this issue Dec 5, 2020 · 1 comment
Labels
bug Something isn't working will accept PR

Comments

@lanmaster
Copy link
Contributor

If i update line chart and invoke 'withMax' with parameter smaller than 1, then YAxis max value is 19.0. There is some example:

public class LineChartExample extends Div {

    private Checkbox checkbox = new Checkbox("Autoupdate chart");


    public LineChartExample() {
        ApexCharts lineChart = ApexChartsBuilder.get()
                .withChart(ChartBuilder.get()
                        .withType(Type.line)
                        .withZoom(ZoomBuilder.get()
                                .withEnabled(true)
                                .build())
                        .build())
                .withStroke(StrokeBuilder.get()
                        .withCurve(Curve.straight)
                        .build())
                .withTitle(TitleSubtitleBuilder.get()
                        .withText("Product Trends by Month")
                        .withAlign(Align.left)
                        .build())
                .withGrid(GridBuilder.get()
                        .withRow(RowBuilder.get()
                                .withColors("#f3f3f3", "transparent")
                                .withOpacity(0.5).build()
                        ).build())
                .withXaxis(XAxisBuilder.get()
                        .withType(XAxisType.datetime)
                        .build())
                // #001. Can i remove this method and use it from update thread? (see #002)
                .withYaxis(YAxisBuilder.get()
                        .withOpposite(true)
                        .withTickAmount(0.1)
                        .withDecimalsInFloat(3.0)
                        .build()
                )
                .withSeries(
                        new Series<>("SAMPLE A",
                                new double[]{0, 0})
                )
                .build();
        add(lineChart);
        add(checkbox);

        setWidth("70%");

        checkbox.setValue(true);


        long schedulerStartedTs = System.currentTimeMillis();
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleAtFixedRate(() -> {

            try {
                double maxValue = Double.MIN_VALUE;
                double minValue = Double.MAX_VALUE;

                if (checkbox.getValue()){
                    //long periodDay = 24 * 60 * 60 * 1000;
                    //long periodHour = 60 * 60 * 1000;
                    //long periodMinute = 60 * 1000;
                    long periodSec = 1000;

                    long periodType = periodSec;
                    int periodsAge = 1000;
                    List<double[]> list = new LinkedList<>();
                    for (int periodCounter = -periodsAge; periodCounter <= 0; periodCounter++) {
                        long ts = System.currentTimeMillis() + periodType * periodCounter;
                        double radians = Math.PI * ts / 180000;

                        double sin = Math.sin(radians * 2);
                        // After 10 seconds a little increase min and max values
                        if ((System.currentTimeMillis() - schedulerStartedTs) > 10000)
                            sin = sin * 1.001;

                        if (sin > maxValue) maxValue = sin;
                        if (sin < minValue) minValue = sin;

                        list.add(new double[]{ts, sin});
                    }

                    Series<Object> series = new Series<>("SAMPLE A", list.toArray());

                    Optional<UI> ui1 = getUI();
                    double finalMaxValue = maxValue;
                    double finalMinValue = minValue;
                    getUI().ifPresent(ui -> {
                        ui.access(() -> {
                            // #002
                            lineChart.setYaxis(YAxisBuilder.get()
                                    // Commented out next 3 lines and moved it to builder (see #001). Why it does't work there?
                                    //.withOpposite(true)
                                    //.withTickAmount(0.1)
                                    //.withDecimalsInFloat(5.0)
                                    .withMax(finalMaxValue)
                                    .withMin(finalMinValue)
                                    .build());

                            lineChart.updateSeries(series);

                            Notification.show("Updated... Min=" + finalMinValue + ". Max=" + finalMaxValue, 10000, Notification.Position.BOTTOM_END);
                        });

                    });

                }

                int q = 0;
            } catch (Throwable e) {
                e.printStackTrace();
            }


        }, 1, 1, TimeUnit.SECONDS);

        addDetachListener(event -> {
            executorService.shutdown();
        });
    }
}

Add LineChartExample object to your UI and start app. At start double sin values are smaller than 1.0 and YAxis look as this:

image

After 10 seconds sin amplitude values will some increase and chart looks another:
image

Desktop (please complete the following information):

  • OS: Windows 7 64x
  • Browser: Chrome 86.0.4240.198
@lanmaster lanmaster added the bug Something isn't working label Dec 5, 2020
@lanmaster
Copy link
Contributor Author

lanmaster commented Dec 5, 2020

Using this dependency:

        <dependency>
            <groupId>com.github.appreciated</groupId>
            <artifactId>apexcharts</artifactId>
            <version>2.0.0.beta10</version>
        </dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working will accept PR
Projects
None yet
Development

No branches or pull requests

2 participants