簡體   English   中英

如何使用Chartkick顯示數據?

[英]How can I get my data to display using Chartkick?

我已經使用各種格式的數據來嘗試獲取Chartkick的多折線圖進行渲染。 我無處可去。 我將粘貼用於返回數據的方法的當前迭代,但知道我已經在整個上午嘗試了各種方法:

def trailing_seven_day_profit(id)
    user = User.find id
    jobs     = Job.select("id")
                 .where("billed != 0.0")
                 .where("start_date >= ?", Date.today - 1.week)
                 .where(user_id: id).to_a
    mon_sell = tue_sell = wed_sell = thu_sell = fri_sell = sat_sell = sun_sell = 0
    mon_cost = tue_cost = wed_cost = thu_cost = fri_cost = sat_cost = sun_cost = 0
    products = Product.where("job_id IN (?)", jobs)
    products.each do |p|
      if p.created_at.strftime("%a") == 'Mon'
        mon_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        mon_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Tue'
        tue_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        tue_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Wed'
        wed_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        wed_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Thu'
        thu_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        thu_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Fri'
        fri_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        fri_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Sat'
        sat_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        sat_cost += (p.quantity * p.cost).to_f
      elsif p.created_at.strftime("%a") == 'Sun'
        sun_sell += (p.price * p.quantity).to_f + (p.tax * (p.price * p.quantity)).to_f
        sun_cost += (p.quantity * p.cost).to_f
      end
    end
    @data = [
      ['Name', 'Day', 'Sell', 'Cost'],
      [user.name, 'Mon', mon_sell.round(2), mon_cost.round(2)],
      [user.name, 'Tue', tue_sell.round(2), tue_cost.round(2)],
      [user.name, 'Wed', wed_sell.round(2), wed_cost.round(2)],
      [user.name, 'Thu', thu_sell.round(2), thu_cost.round(2)],
      [user.name, 'Fri', fri_sell.round(2), fri_cost.round(2)],
      [user.name, 'Sat', sat_sell.round(2), sat_cost.round(2)],
      [user.name, 'Sun', sun_sell.round(2), sun_cost.round(2)]
    ]
    @data
  end

我的數據庫有一些解釋-喬布斯有很多產品。 產品記錄保存着添加到作業中的物料的成本,售價(變量),稅金,數量以及與作業的關系,可以得到物料描述的存貨等,以及一些時間戳。

因此,對於具有三個產品的Job#1,我們需要Job.find(1).products.each每個都可以看到信息。

或Product.find(“(?)中的job_id”,[array_of_job_ids])這就是我在這里采用的方法。

我過去曾經有過這份報告,但是使用Google圖表完全是手動的,因此代碼太可怕了。 因此,我找到了Chartkick,並且我喜歡將視圖與所做的操作進行比較。

現在我什么也無法渲染。

該視圖應盡可能簡單:

              <% @users.each do |u| %>
                <div class="widget-content">
                  <%= line_chart name: u.name, data: ReportMaker.new.trailing_seven_day_profit(u.id) %>
                </div>
              <% end %>

但這沒有任何意義。

我已經嘗試過as_json,嘗試過作為哈希,它目前是數組格式,但是數據永遠不會出現,圖例也不會出現。

因此,很明顯,出了點問題。 但是google之類的類似“ chartkick多系列格式”的東西,您會得到很多真正無益的結果-Chartkick頁面上的文檔(對不起,伙計)很爛。

他說:

<%= line_chart [
  {name: "Series A", data: series_a},
  {name: "Series B", data: series_b}
] %>

那么wtf應該是series_a的樣子嗎? JSON? 哈希? AR對象? 數組?

我沒有做簡單的事情,例如所有示例所示的“工作數量”或“按created_at分組產品”。

我必須遍歷集合,提取值並組合它們,然后呈現一些數據集。

還有-產品的干燥感。下面的每個方塊都傷了我的心。 很高興接受有關如何將其干燥的任何指示信息:)

products.each塊很難看,因為此工作應在數據庫級別完成。 您需要這樣的東西:

def trailing_seven_day_profit(id)
  Product.connection.select_all('
    SELECT u.name, DAYNAME(p.created_at) AS day, 
      p.price * p.quantity + p.tax * p.price * p.quantity AS sell, 
      p.quantity * p.cost AS cost
    FROM products p
    INNER JOIN jobs j ON j.id = p.job_id
    INNER JOIN users u ON u.id = j.user_id
    WHERE j.billed != 0 AND j.start_date >= CURDATE() - INTERVAL 1 WEEK
    WHERE u.id = #{id.to_i}
    GROUP BY DAYNAME(p.created_at)
  '
end

按OP編輯:

我修改了您的查詢以獲取任何結果:

SELECT to_char(p.created_at, 'day') AS day_of_week,
               p.price * p.quantity + p.tax * p.price * p.quantity AS sell,
               p.quantity * p.cost AS total_cost
FROM products p
  INNER JOIN jobs j ON j.id = p.job_id
  INNER JOIN users u ON u.id = j.user_id
WHERE j.billed != 0 AND j.start_date >= CURRENT_DATE - INTERVAL '7 DAY' AND u.id = 242
GROUP BY to_char(p.created_at, 'day'), sell, total_cost

但這會打印出完全不正確的內容:

SQL查詢結果

我希望將一周的7天作為不同的行進行跟蹤,以匯總成本和售價。 我將為每個用戶抓取該查詢,以獲取所需的Chartkick數據。 我不是數據庫向導,並且想了解更多有關此可能解決方案的信息。

在這里編輯,因為我的評論過長...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM