散景折线图不显示折线

Bokeh Line Chart not displaying Lines

提问人:developer 提问时间:11/12/2023 最后编辑:EricLavaultdeveloper 更新时间:11/13/2023 访问量:61

问:

我正在尝试创建一个应用程序,该应用程序将根据所选项目显示一些图表,以获取平均月度数据,例如“6/2023”、“7/2023”。我有正在工作的代码,我可以在我的终端中看到输出,但折线图不起作用,只是一个空白页,看起来它没有接收到我的数据。请帮忙

class Item(models.Model):
    item = models.CharField(max_length=100)
    date = models.DateField(null=True)
    price = models.FloatField()

    def __str__(self):
        return self.item
def line(request):
    items = Item.objects.values_list('item', flat=True).distinct()
    item = request.GET.get('item', 'Drink')
    data = Item.objects.filter(item=item).annotate(
        month=F('date__month'),
        year=F('date__year')
    ).values(
        'month',
        'year'
    ).annotate(
        avg_price=Avg('price')
    ).order_by(
        'year',
        'month'
    )

    item_years = [f"{k['month']}/{k['year']}" for k in data]
    item_price = [f"{v['avg_price']}" for v in data]

    cds = ColumnDataSource(data=dict(item_years=item_years, item_price=item_price))
    fig = figure(height=500, sizing_mode="stretch_width", title=f"{item} GDP")
    fig.title.align = 'center'
    fig.title.text_font_size = '1.5em'
    fig.yaxis[0].formatter = NumeralTickFormatter(format="$0.0a")

    fig.circle(source=cds, x='item_years', y='item_price', size=12)
    fig.line(source=cds, x='item_years', y='item_price', line_width=2)

    tooltips = [
        ('Year', '@item_years'),
        ('Price', '@item_price')
    ]
    fig.add_tools(HoverTool(tooltips=tooltips))

    script, div = components(fig)
    context = {
        'items': items,
        'item': item,
        'script': script,
        'div': div,
    }

    if request.htmx:
        return render(request, 'analysis/partials/gdp-bar.html', context)

    return render(request, 'analysis/line.html', context)
<div class="row">
        <div id="linechart" class="col-10">
            {% include 'analysis/partials/gdp-bar.html' %}
        </div>
        <div class="col-2">
                <label>Product</label>
                <select id="select-year"
                        class="custom-select"
                        name="item"
                        autocomplete="off"
                        hx-get="{% url 'linechart' %}"
                        hx-target="#linechart">

                    {% for c in items %}
                        <option value="{{c}}"
                        {% if item == c %} selected {% endif %}>{{c}}</option>
                    {% endfor %}
                </select>

我尝试在折线图中显示数据,如果我只使用年或月数据,它就可以工作,但不能同时使用月和是的。

蟒蛇 python-3.x django 散景

评论

0赞 Community 11/13/2023
请修剪您的代码,以便更轻松地找到您的问题。请遵循这些准则,以创建最小的可重现示例
0赞 bigreddot 11/14/2023
您正在将字符串数据放入 CDS,这意味着所有数据都被视为分类因子,并且您需要定义一个分类范围,如在 docs.bokeh.org/en/latest/docs/user_guide/topics/...但据推测,您实际上并不希望这样做,在这种情况下,您应该将数据作为实际时间戳和数字放入 CDS 中。
0赞 developer 11/14/2023
@bigreddot我首先使用“item_years = [d.date for d in data]”,它会在终端中为我提供数据,但它也不会创建折线图。如果我执行“”item_years = [d.date.year for d in data]“,它就可以工作,但它有效,但只显示年份。但是如何显示年份和月份呢?谢谢你的任何建议

答:

-1赞 BishalTx 11/12/2023 #1

如果我是你,我会为每个数据数组返回JSON格式的数据,并使用chart.js(最新版本)。