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

Implement SparklineBarFormatter to customise bar sparklines #216

Open
amp36 opened this issue Jul 7, 2023 · 4 comments
Open

Implement SparklineBarFormatter to customise bar sparklines #216

amp36 opened this issue Jul 7, 2023 · 4 comments
Labels
feature something new P3 backlog

Comments

@amp36
Copy link

amp36 commented Jul 7, 2023

We already have a valueFormatter, it would be nice to be able to customise sparklines using a formatter in sparklineOptions
https://www.ag-grid.com/javascript-data-grid/sparklines-bar-customisation/#barsparklineoptions

@amp36 amp36 changed the title Implement SparklineBarFormatter to customise bar sparklines [Feature Request] Implement SparklineBarFormatter to customise bar sparklines Jul 7, 2023
@BSd3v
Copy link
Collaborator

BSd3v commented Jul 7, 2023

Hello @amp36,

Please check out here to see if this helps.

https://www.ag-grid.com/react-data-grid/sparklines-data/#formatting-sparkline-data

This isnt really something that we can add to the grid, but would have to be something that ag-grid handles itself.

@amp36
Copy link
Author

amp36 commented Jul 10, 2023

It seems like I should be able to pass a formatter function into sparklineOptions in columnDefs like this:

columnDefs = [
    {'field': 'direction'},
    {
        'field': 'frequency',
        "cellRenderer": "agSparklineCellRenderer",
        "cellRendererParams": {
            "sparklineOptions": {
                "type": "bar",
                "formatter": {"function": 'return { "fill": params.yValue < 0 ? "#ab3326" : "#399e1b" };'},
                "fill": '#5470c6',
                "stroke": '#91cc75',
                "highlightStyle": {
                    "fill": '#fac858'
                },
                "valueAxisDomain": [-50, 50],
            },
        },
    },
]

However, the error I get in the console is that Uncaught TypeError: o is not a function, and it appears that the function is being passed as an object rather than as valid javascript by dash

@BSd3v
Copy link
Collaborator

BSd3v commented Jul 10, 2023

Ok, yes, I see the issue now.

We will need to add this functionality in our next release. Probably going to try to work with more Enterprise features.

@AnnMarieW
Copy link
Collaborator

AnnMarieW commented Jul 10, 2023

Hi @amp36

You can make the whole cellRendererParams a function. Below is a complete minimal example.

Note that you can also use components such as dbc.Progress. You can find an example in the preliminary docs (it hasn't been added to the dash docs yet) https://dashaggrid.pythonanywhere.com/components/cell-renderer


SparlinkeBarFormatter example:

image

import dash_ag_grid as dag
from dash import Dash, html

app = Dash(__name__)

rowData = [
    {
        "symbol": "A",
        "name": "Agilent Technologies Inc. Common Stock",
        "change": [0.9, 0.8],
        "volume": "$971,760",
    },
    {
        "symbol": "AAL",
        "name": "American Airlines Group Inc. Common Stock",
        "change": [0.2, 0.3],
        "volume": "$20,309,670",
    },
    {
        "symbol": "AAP",
        "name": "Advance Auto Parts Inc Advance Auto Parts Inc W/I",
        "change": [0.4, 0.1],
        "volume": "$699,427",
    },
    {
        "symbol": "AAPL",
        "name": "Apple Inc. Common Stock",
        "change": [0.5, 0.7],
        "volume": "$57,807,909",
    },
]

columnDefs = [
    {"field": "symbol", "maxWidth": 120},
    {"field": "name", "minWidth": 250},
    {
        "field": "change",
        "cellRenderer": "agSparklineCellRenderer",
        "cellRendererParams": {"function": "sparklineBarCellRendererParams"},
    },
    {
        "field": "volume",
        "type": "numericColumn",
        "maxWidth": 140,
    },
]

defaultColDef = {
    "flex": 1,
    "minWidth": 100,
    "resizable": True,
}


app.layout = html.Div(
    [
        dag.AgGrid(
            enableEnterpriseModules=True,
            rowData=rowData,
            defaultColDef=defaultColDef,
            columnDefs=columnDefs,
            columnSize="sizeToFit",
            dashGridOptions={"rowHeight": 50},
        ),
    ],
    style={"margin": 20},
)

if __name__ == "__main__":
    app.run(debug=True)


"""
Add the following to the dashAgGridFunctions.js file in  /assets


var dagfuncs = (window.dashAgGridFunctions = window.dashAgGridFunctions || {});

function sparklineBarFormatter(params) {
  const { yValue } = params;
  return {
    fill: yValue <= .20 ? '#4fa2d9' : yValue < .60 ? '#277cb5' : '#195176',
  };
}

dagfuncs.sparklineBarCellRendererParams = {
    sparklineOptions: {
      type: 'bar',
    //  fill: '#5470c6',
      stroke: '#91cc75',
      highlightStyle: {
        fill: '#fac858',
      },
      valueAxisDomain: [0, 1],
      paddingOuter: 0,
      padding: {
        top: 0,
        bottom: 0,
      },
      axis: {
        strokeWidth: 0,
      },
      formatter: sparklineBarFormatter
    },
  }




"""

@BSd3v BSd3v added the documentation written for humans label Oct 13, 2023
@gvwilson gvwilson assigned gvwilson and unassigned gvwilson Jul 27, 2024
@gvwilson gvwilson changed the title [Feature Request] Implement SparklineBarFormatter to customise bar sparklines Implement SparklineBarFormatter to customise bar sparklines Aug 13, 2024
@gvwilson gvwilson added feature something new P3 backlog and removed documentation written for humans labels Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature something new P3 backlog
Projects
None yet
Development

No branches or pull requests

4 participants