Beispiel #1
0
def test_dimension_func(monkeypatch, ndx):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")

    dat = ndx

    fluctuation_eq = abs(VC("close") - VC("open"))

    month_dim = Dimension("month", group_type="sum", func=fluctuation_eq)

    assert 'Math.abs(d.close - d.open)' == month_dim.func

    expected_str = '''
    var month_dimension = facts.dimension(function(d){return d['month'];});
    
    var month_group = month_dimension.group().reduceSum(function(d){
        return Math.abs(d.close - d.open);
    });
    '''

    expected_str_replaced = str(expected_str).replace("\n", "").replace(
        " ", "").replace("\t", "")
    asserted_str_replaced = str(month_dim).replace("\n", "").replace(
        " ", "").replace("\t", "")

    assert expected_str_replaced == asserted_str_replaced
Beispiel #2
0
def test_gainOrLossChart(ndx):
    def gainOrLoss(x):
        if x < 0:
            return "Loss"
        else:
            return "Gain"

    dat = ndx
    dat["change"] = dat.close - dat.open
    dat["gainOrLoss"] = dat.change.apply(gainOrLoss)
    gainOrLoss_dim = Dimension("gainOrLoss")

    gainOrLossChart = PieChart(
        "gain-loss-chart",
        gainOrLoss_dim,
        width=180,
        height=180,
        radius=80,
        inner_radius=40,
        renderLabel=True,
        label=Label("percent", precision=0),
        transitionDuration=500,
        colors=["#3182bd", "#6baed6", "#9ecae1", "#c6dbef", "#dadaeb"],
        colorDomain=[-1750, 1644],
        colorAccessor=True,
    )

    dc_documentation_string = """
    var pie_chart_gain_loss_chart = dc.pieChart("#gain-loss-chart")
            .width(180)
            .height(180)
            .radius(80) 
            .dimension(gainOrLoss_dimension)
            .group(gainOrLoss_group)
            .renderLabel(true) 
            .innerRadius(40) 
            .transitionDuration(500) 
            .colors(['#3182bd', '#6baed6', '#9ecae1', '#c6dbef', '#dadaeb']) 
            .colorDomain([-1750, 1644]) 
            .colorAccessor(function(d, i){return d.value;});

    pie_chart_gain_loss_chart.label(function (d) {
                if (pie_chart_gain_loss_chart.hasFilter() && !pie_chart_gain_loss_chart.hasFilter(d.key)) {
                    return d.key + '(0%)';
                }
                var label = d.key;
                if (all.value()) {
                    label += '(' + Math.floor(d.value / all.value() * 100)/1 + '%)';
                }
                return label;
            });
            """

    gainOrLossChart_replaced = (str(gainOrLossChart).replace("\n", "").replace(
        " ", "").replace("\t", ""))
    dc_documentation_string_replaced = (str(dc_documentation_string).replace(
        "\n", "").replace(" ", "").replace("\t", ""))

    assert gainOrLossChart_replaced == dc_documentation_string_replaced
Beispiel #3
0
def test_dayOfWeekChart(ndx):
    def day_of_week(x):
        y = pd.to_datetime(x)
        return day_list[y.dayofweek]

    dat = ndx

    day_list = [
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday",
    ]

    dat["day_of_week"] = dat.date.apply(day_of_week)
    day_of_week_dim = Dimension("day_of_week")
    row_chart = RowChart(
        "day-of-week-chart",
        day_of_week_dim,
        elasticX=True,
        height=180,
        width=180,
        xAxis="ticks(4)",
        label=Label("key", part=0),
    )

    dc_documentation_string = """
        var row_chart_day_of_week_chart = dc.rowChart("#day-of-week-chart")
            .width(180)
            .height(180)
            .margins({top: 20, left: 10, right: 10, bottom: 20})
            .group(day_of_week_group)
            .dimension(day_of_week_dimension)
            .ordinalColors(['#3182bd', '#6baed6', '#9ecae1', '#c6dbef', '#dadaeb'])
            .title(function (d) {
                return d.value;
            })
            .elasticX(true)
            .label(function (d) {
                return d.key[0];
            });
            
        row_chart_day_of_week_chart.xAxis().ticks(4);
        
    """

    row_chart_replaced = (str(row_chart).replace("\n",
                                                 "").replace(" ", "").replace(
                                                     "\t", ""))
    dc_documentation_string_replaced = (str(dc_documentation_string).replace(
        "\n", "").replace(" ", "").replace("\t", ""))

    assert row_chart_replaced == dc_documentation_string_replaced
Beispiel #4
0
def test_fluctuationChart(ndx):
    dat = ndx

    dat["fluctuation"] = np.round((dat.close - dat.open) / dat.open * 100)

    fluctuation_dim = Dimension("fluctuation")

    fluctuation_chart = BarChart(
        "volume-month-chart",
        fluctuation_dim,
        width=420,
        height=180,
        elasticY=True,
        gap=1,
        centerBar=True,
        alwaysUseRounding=True,
        round='dc.round.floor',
        margins=Margin(top=10, right=50, bottom=30, left=40),
        xAxis="tickFormat(function(v){return v+'%';})",
        yAxis="ticks(5)",
        x='d3.scaleLinear().domain([-25, 25])',
        filter_printer=True)

    dc_documentation_string = """
    var bar_chart_volume_month_chart = dc.barChart("#volume-month-chart")
        .width(420)
        .height(180)
        .margins({top: 10, right: 50, bottom: 30, left: 40})
        .dimension(fluctuation_dimension)
        .group(fluctuation_group)
        .elasticY(true)
        .centerBar(true)
        .gap(1)
        .round(dc.round.floor)
        .alwaysUseRounding(true)
        .renderHorizontalGridLines(true)
        .x(d3.scaleLinear().domain([-25, 25]))
        .filterPrinter(function (filters) {
            var filter = filters[0], s = '';
            s += numberFormat(filter[0]) + '% -> ' + numberFormat(filter[1]) + '%';
            return s;
        });
        
    bar_chart_volume_month_chart.xAxis().tickFormat(
        function (v) { return v + '%'; });
    
    bar_chart_volume_month_chart.yAxis().ticks(5);
    """

    row_chart_replaced = (str(fluctuation_chart).replace("\n", "").replace(
        " ", "").replace("\t", ""))
    dc_documentation_string_replaced = (str(dc_documentation_string).replace(
        "\n", "").replace(" ", "").replace("\t", ""))

    assert row_chart_replaced == dc_documentation_string_replaced
Beispiel #5
0
def test_volume_chart():
    monthDim = Dimension('month',
                         group="volume",
                         group_type='sum',
                         modifier='/500000')

    stacked_area_range = BarChart(
        "stacked_area_range",
        monthDim,
        width=990,
        height=40,
        elasticY=True,
        alwaysUseRounding=True,
        round='d3.timeMonth.round',
        gap=1,
        centerBar=True,
        xUnits='d3.timeMonths',
        margins=Margin(0, 50, 20, 40),
        renderHorizontalGridLines=False,
        x='d3.scaleTime().domain([new Date(1985, 0, 1), new Date(2012, 11, 31)])'
    )

    expected = '''var bar_chart_stacked_area_range = dc.barChart("#stacked_area_range")
        .width(990)
        .height(40)
        .margins({top: 0, right: 50, bottom: 20, left: 40})
        .dimension(month_dimension)
        .group(month_group)
        .elasticY(true)
        .centerBar(true)
        .gap(1)
        .round(d3.timeMonth.round)
        .xUnits(d3.timeMonths)
        .alwaysUseRounding(true)
        .x(d3.scaleTime().domain([new Date(1985, 0, 1), new Date(2012, 11, 31)]));
        '''

    assert str(stacked_area_range).replace("\n", "").replace(" ", "").replace(
        "\t", "") == str(expected).replace("\n",
                                           "").replace(" ",
                                                       "").replace("\t", "")
Beispiel #6
0
def bar_chart(ndx):
    dat = ndx

    dat["fluctuation"] = np.round((dat.close - dat.open) / dat.open * 100)

    fluctuation_dim = Dimension("fluctuation")

    fluctuation_chart = BarChart(
        "volume-month-chart",
        fluctuation_dim,
        width=420,
        height=180,
        elasticY=True,
        gap=1,
        centerBar=True,
        alwaysUseRounding=True,
        xAxis="tickFormat(function(v){return v+'%';})",
        yAxis="ticks(5)",
    )

    return fluctuation_chart
Beispiel #7
0
from pydashboard import components as s
from pydashboard.example_data.example_data import dat
from pydashboard.components import Dimension, MultiDimension
from pydashboard.dc_components import PieChart, RowChart, ScatterPlot
from pydashboard.dominate_template import dashboard2 as t


dim_address = Dimension("address")
dim_Pstatus = Dimension("Pstatus")
dim_famsize = Dimension("famsize")
dim_romantic = Dimension("romantic")
dim_studytime = Dimension("studytime")
dim_age_absences = MultiDimension("age", "absences")

# pie_chart = PieChart("A", dim_address, radius=200, height=500, width=500)
pie_chart2 = PieChart("B", dim_Pstatus, radius=100, slicesCap=2)
pie_chart3 = PieChart("C", dim_famsize, radius=100, inner_radius=20)
pie_chart4 = PieChart("D", dim_romantic)
pie_chart5 = RowChart(
    "E", dim_studytime, elasticX=True, xAxis="tickValues([0, 20, 200])"
)
pie_chart6 = PieChart("F", dim_romantic)
pie_chart7 = PieChart("G", dim_romantic)
pie_chart8 = PieChart("H", dim_romantic)
pie_chart9 = PieChart("I", dim_romantic)

scatter_plot = ScatterPlot("A", dim_age_absences)


dashboard = s.Dashboard(
    scatter_plot,
Beispiel #8
0

dat.head()
dat["fluctuation"] = np.round((dat.close - dat.open) / dat.open * 100)


def get_year(x):
    return x.split("/")[2]


dat["gain"] = dat.change.apply(gainOrLoss)
dat["year"] = dat.date.apply(get_year)
dat["quarter"] = dat.date.apply(get_quarter)
dat["day_of_week"] = dat.date.apply(day_of_week)

gain_dim = Dimension("gain")
fluctuation_dim = Dimension("fluctuation")
day_of_week_dim = Dimension("day_of_week")
quarter_dim = Dimension("quarter", group="volume", group_type="sum")

gain_loss_chart = PieChart(
    "gain_loss_chart",
    gain_dim,
    radius=80,
    width=180,
    height=180,
    label=Label("percent", precision=0),
)

quarter_chart = PieChart("quarters",
                         quarter_dim,