mirror of
https://github.com/nprasad2077/nbaStats.git
synced 2026-11-04 09:15:35 +00:00
add calculation,view,url for PTS histogram
This commit is contained in:
@@ -6,3 +6,8 @@ class PlayerDataSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = PlayerData
|
||||
fields = '__all__'
|
||||
|
||||
class HistogramDataSerializer(serializers.Serializer):
|
||||
range = serializers.CharField()
|
||||
count = serializers.IntegerField()
|
||||
|
||||
|
||||
+2
-1
@@ -1,5 +1,5 @@
|
||||
from django.urls import path
|
||||
from .views import PlayerDataList, PlayerDataByTeamList, PlayerDataBySeasonList, PlayerDataByNameList, TopScorersbySeasonList, ThreeTwoTopPointTrends, TopAssistsBySeasonList, TopReboundsBySeasonList, TopBlocksBySeasonList, TopStealsBySeasonList, TopOffensiveReboundsBySeasonList, TopDefensiveReboundsBySeasonList
|
||||
from .views import PlayerDataList, PlayerDataByTeamList, PlayerDataBySeasonList, PlayerDataByNameList, TopScorersbySeasonList, ThreeTwoTopPointTrends, TopAssistsBySeasonList, TopReboundsBySeasonList, TopBlocksBySeasonList, TopStealsBySeasonList, TopOffensiveReboundsBySeasonList, TopDefensiveReboundsBySeasonList, PointsPerGameHistogramBySeasonList
|
||||
|
||||
urlpatterns = [
|
||||
path('api/playerdata/', PlayerDataList.as_view(), name='playerdata_list'),
|
||||
@@ -14,4 +14,5 @@ urlpatterns = [
|
||||
path('api/top_steals/<int:season>/', TopStealsBySeasonList.as_view(), name='top_steals'),
|
||||
path('api/top_rebounds_offensive/<int:season>/', TopOffensiveReboundsBySeasonList.as_view(), name='top_rebounds_ORB'),
|
||||
path('api/top_rebounds_defensive/<int:season>/', TopDefensiveReboundsBySeasonList.as_view(), name='top_rebounds_DRB'),
|
||||
path('api/points_per_game_histogram/<int:season>/', PointsPerGameHistogramBySeasonList.as_view(), name='points_per_game_histogram'),
|
||||
]
|
||||
+34
-1
@@ -3,10 +3,12 @@ from django.http import JsonResponse
|
||||
from django.core import serializers
|
||||
from .models import PlayerData
|
||||
from rest_framework import generics
|
||||
from .serializers import PlayerDataSerializer
|
||||
from .serializers import PlayerDataSerializer, HistogramDataSerializer
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from django.db.models import Avg
|
||||
from collections import Counter
|
||||
import math
|
||||
|
||||
|
||||
# Create your views here.
|
||||
@@ -141,3 +143,34 @@ class TopDefensiveReboundsBySeasonList(generics.ListAPIView):
|
||||
def get_queryset(self):
|
||||
season = self.kwargs['season']
|
||||
return PlayerData.objects.filter(season=season).order_by('-DRB')[:20]
|
||||
|
||||
# PTS Histogram
|
||||
|
||||
|
||||
class PointsPerGameHistogramBySeasonList(generics.ListAPIView):
|
||||
serializer_class = HistogramDataSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
season = self.kwargs['season']
|
||||
queryset = PlayerData.objects.filter(season=season)
|
||||
|
||||
# Get the points per game for each player
|
||||
points_per_game_list = [float(player.PTS) for player in queryset]
|
||||
|
||||
# Define histogram bins (e.g., 0-5, 5-10, 10-15, etc.)
|
||||
bin_ranges = range(0, int(math.ceil(max(points_per_game_list))) + 1, 5)
|
||||
histogram_data = Counter()
|
||||
|
||||
# Fill the histogram with data
|
||||
for ppg in points_per_game_list:
|
||||
bin_index = next(
|
||||
(i for i, r in enumerate(bin_ranges) if r >= ppg), -1)
|
||||
if bin_index >= 1:
|
||||
bin_label = f"{bin_ranges[bin_index - 1]}-{bin_ranges[bin_index]}"
|
||||
histogram_data[bin_label] += 1
|
||||
|
||||
# Convert histogram_data to list of dicts for serialization
|
||||
histogram_list = [{"range": k, "count": v}
|
||||
for k, v in histogram_data.items()]
|
||||
|
||||
return histogram_list
|
||||
|
||||
Reference in New Issue
Block a user