ecomfe / echarts-stat

Statistics tool for Apache ECharts
593 stars 309 forks source link

Missing type for transform regression. #35

Open y0nd0 opened 3 years ago

y0nd0 commented 3 years ago

How to use transform regression in echarts (ngx-echarts) TypeScript? Missing type?

import * as echarts from 'echarts';
import * as ecStat from 'echarts-stat';
echarts.registerTransform(ecStat.transform.regression);

image

ecStat.d.ts

// node_modules\echarts-stat\src\ecStat.d.ts

declare namespace EChartsStat {
  type InputData = Array<Array<number>>;
  type OutputData = Array<Array<number>>;

  interface HistogramBins {
    bins: Array<HistogramBinsBin>;
    data: OutputData;
    customData: OutputData;
  }
  interface HistogramBinsBin {
    x0: number;
    x1: number;
    sample: Array<number>;
  }
  function histogram(
    data: Array<number>,
    binMethod: 'squareRoot' | 'scott' | 'freedmanDiaconis' | 'sturges'
  ): HistogramBins;

  namespace clustering {
    interface Result {
      centroids: OutputData;
      clusterAssment: OutputData;
      pointsInCluster: OutputData;
    }
    function hierarchicalKMeans(
      data: InputData,
      clusterNumer: number,
      stepByStep: boolean
    ): Result;
    function kMeans(data: InputData, clusterNumer: number): Result;
  }

  interface RegressionResult {
    points: OutputData;
    expression: string;
    gradient: number;
    intercept: number;
  }
  function regression(
    regreMethod: 'linear' | 'exponential' | 'logarithmic' | 'polynomial',
    data: InputData,
    order: number
  ): RegressionResult;

  namespace statistics {
    function deviation(data: Array<number>): number;
    function sampleVariance(data: Array<number>): number;
    function quantile(data: Array<number>, p: number): number;
    function max(data: Array<number>): number;
    function mean(data: Array<number>): number;
    function median(data: Array<number>): number;
    function min(data: Array<number>): number;
    function sum(data: Array<number>): number;
  }
}

declare module 'echarts-stat' {
  export = EChartsStat;
}

Goal: https://echarts.apache.org/examples/en/editor.html?c=scatter-linear-regression

{
    "echarts": "^5.0.2",
    "echarts-stat": "^1.2.0",
    "ngx-echarts": "^6.0.1",
}
GreedyPirate commented 3 years ago

same issue, @100pah Is there a solution? @y0nd0 have you solved the problem?

dvago commented 3 years ago

@y0nd0 @GreedyPirate this way works for me:

import { transform } from 'echarts-stat'

echarts.registerTransform(transform.regression)

which is even better 'cause you don't need to import the whole ecStat package if you are going to use just one transformation.

nico-campa commented 3 years ago

same issue, there is no 'transform' available from echarts-stat package

my packages versions : "echarts": "^5.2.0", "echarts-stat": "^1.2.0", "ngx-echarts": "^7.0.1",

anyone solved the problem ?

coader commented 3 years ago

any news? the same problem

CDFO2 commented 3 years ago

same issue, any suggestions please

CDFO2 commented 3 years ago

@y0nd0 @GreedyPirate this way works for me:

import { transform } from 'echarts-stat'

echarts.registerTransform(transform.regression)

which is even better 'cause you don't need to import the whole ecStat package if you are going to use just one transformation.

there is no 'transform' available from echarts-stat package. Which version do you use please? @dvago

dvago commented 3 years ago

@CDFO2

"echarts": "^5.1.2",
"echarts-stat": "^1.2.0",
CDFO2 commented 3 years ago

@dvago I'm having below versions.

    "echarts": "^5.2.1",
    "echarts-stat": "^1.2.0",

I've tried downgrading echarts to 5.1.2, but then its giving following error.

Property 'registerTransform' does not exist on type 'typeof echarts'

dvago commented 3 years ago

@CDFO2 are you importing the library as:

import * as echarts from 'echarts'

Note: the above is bad practice but it's a work around.

CDFO2 commented 3 years ago

@dvago Yes, following exactly the same as mentioned here.

import * as echarts from 'echarts';

import ecStat from 'echarts-stat';

var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
var option;
echarts.registerTransform(ecStat.transform.regression);
dvago commented 3 years ago

@CDFO2 I would try with this and see what happens:

import * as echarts from 'echarts';
import { transform } from 'echarts-stat';

echarts.registerTransform(transform.regression);

var myChart = echarts.init(document.getElementById('main'));
var option; // whatever option you want to pass
myChart.setOption(option);

This sequence (importing the library, registering the transformation and init the chart) has been provided by one of the library maintainer a few months ago.

I'm not fully sure if this helps out 'cause the error says "echarts doesn't contain the function registerTransform so you can't call it" (this method is not even documented within echarts website https://echarts.apache.org/en/api.html#echarts)

I've been raising a proposal and this guy opened an issue related to this registerTransform but it still open: #15124

nflux-pyang commented 2 years ago

Any update for the transform problem?

davidcanonieto commented 2 years ago

Come on guys! when are you going to fix this?

maneetgoyal commented 2 years ago

A fix would be really helpful!!

maneetgoyal commented 2 years ago

Resorting to module augmentation till a fix is available:

import type { ExternalDataTransform } from "@manufac/echarts-simple-transform";

/**
 * Needed because of: https://github.com/ecomfe/echarts-stat/issues/35
 * Module augmentation: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
 */
declare module "echarts-stat" {
  let transform: {
    regression: ExternalDataTransform;
    histogram: ExternalDataTransform;
    clustering: ExternalDataTransform;
  };
}

Took some hints from https://github.com/ecomfe/echarts-stat/pull/41 as well.

tlianglstyle commented 2 years ago

temporary solution: const ecStatLocal: any = ecStat; echarts.registerTransform(ecStatLocal.transform.regression);

or:https://github.com/ecomfe/echarts-stat/pull/41

macchif commented 2 years ago

I had the same issue and i found this solution:

I replaced import ecStat from 'echarts-stat'; with import * as ecStat from 'echarts-stat';

and echarts.registerTransform(ecStat.transform.regression); with echarts.registerTransform(ecStat['transform'].regression);

in this way everything works as it should, but I have no idea of the reason why.

Anyway I hope it helps ;)

hwebb commented 1 year ago

The above doesn't work anymore because of TypeScript, you can try this instead:

import { registerTransform } from 'echarts';

import * as ecStat from 'echarts-stat';

registerTransform((ecStat as any).transform.regression);
0aryan commented 1 year ago

Found Another way of implementing in Angular

Run Register Transform inside lifecycle hook.


import * as echarts from 'echarts'
import * as ecStat from 'echarts-stat';

 ngOnInit () : void {
echarts.registerTransform((ecStat as any).transform.regression);
}
echodis commented 1 year ago

您好,这里是周敏。您的来信我已收到,我会在查看后的第一时间给你回复。

dcantu96 commented 1 year ago
import { transform } from 'echarts-stat'

this should be the correct solution, the current solutions are not tree shakeable

oleksandr-kupenko commented 1 year ago

If you are interested in regression, you can use my script. I took and rewrote several files from the library and now I don’t need to install the entire library. This solved the problem with imports. Most likely it will not work in strict typescript mode. But in normal mode everything works.

https://github.com/oleksandr-kupenko/echarts-stat-regression

echodis commented 1 year ago

您好,这里是周敏。您的来信我已收到,我会在查看后的第一时间给你回复。

kraisorna commented 5 months ago

temporary solution: const ecStatLocal: any = ecStat; echarts.registerTransform(ecStatLocal.transform.regression);

or:#41

this works. i imported the ecStat and change it to any type. and follow the rest.

import * as ecStat from 'echarts-stat';

const ecStatLocal: any = ecStat; echarts.registerTransform(ecStatLocal.transform.histogram);

yuexiliuli commented 4 months ago

临时解决方案: const ecStatLocal: any = ecStat; echarts.registerTransform(ecStatLocal.transform.regression);

或:#41

It's useful,thanks.