nicolaskruchten / pivottable

Open-source Javascript Pivot Table (aka Pivot Grid, Pivot Chart, Cross-Tab) implementation with drag'n'drop.
https://pivottable.js.org/
MIT License
4.33k stars 1.07k forks source link

colorScaleGenerator in heatmap for Python #1337

Closed umitsarioz closed 1 year ago

umitsarioz commented 1 year ago

Hi Nicolas! Thanks for your effort and this repo. I can not change heatmap color scale with Python. I read your documentation and tried renderers parameter. Renderers parameter for heatmap does not work for python i guess. (colorScaleGenerator) Does the repo have the capacity to do this job, with the renderers parameter etc.? I cannot actually, do you have any idea how can i achieve it?

juanmantelli commented 1 year ago

Can you share your code?

heatmap: {
        colorScaleGenerator: function(values) {
            return Plotly.d3.scale.linear()
                .domain([-100, -1, 0, 1, 200])
                .range(["#a0432c", "#a37265", "#dbdbdb", "#b0c67a", "#68c44f"]);
        }
    },

that should work.

umitsarioz commented 1 year ago

Yes, of course. My code is like that:

import pandas as pd 
import pivottablejs

def pivot_ui(df, **kwargs):
    # Function redefine to remove null values in pivot table
    class _DataFrame(pd.DataFrame):
        def to_csv(self, **kwargs):
            return super().to_csv(**kwargs).replace("\r\n", "\n")
    return pivottablejs.pivot_ui(_DataFrame(df), **kwargs)

def main(src_filepath:str,dst_filepath="avg_price_pivot.html"):
    df = pd.read_csv(src_filepath) # read data as csv

    # convert to pivot and save it as html
    pivot_ui(df=df,
             rows=["Year"],
             cols=["Brand"],
             aggregatorName="Average",
             vals=["Price"],
             rendererName="Heatmap",
             hiddenFromDragDrop=["Price"],
             hiddenFromAggregators=[""],
             outfile_path=dst_filepath)
    print(f">> Pivot table created as {dst_filepath}")

# Run code
main(src_filepath="mini_train.csv")

And my data format is like that, a mini sample is below . After all, i want to change background of heatmap yellow to green instead of only red.

Brand Model Year Mileage Price
BMW 225 2004 190000 8467
BMW 528 2008 280000 17876
HYUNDAI Tucson 2012 151995 24543
FORD C-MAX 2013 74000 20385
HYUNDAI Elantra 2011 137213 23239
HYUNDAI Sonata 2016 240573 19914
HYUNDAI H1 2008 234278 13589
MERCEDES-BENZ E 350 2010 205968 1646
TOYOTA CHR 2018 46733 549
MERCEDES-BENZ Sprinter 2006 37440 35850
umitsarioz commented 1 year ago

solved with implemented js to generated html file with python.